1

I've tried the following methods but none would work on Linux

from win32.win32api import GetSystemMetrics


print(GetSystemMetrics(0), GetSystemMetrics(1))
from ctypes import windll


info = windll.user32


print(info.GetSystemMetrics(0), info.GetSystemMetrics(1))
3
  • Tried using tkinter? from tkinter import * and root = Tk() and root.withdraw() and root.winfo_screenheight() and root.winfo_screenwidth() Commented Oct 3, 2020 at 22:02
  • @CoolCloud If you've got a good solution with tkinter, it would be worth posting an answer. I posted one using xlib but that is not in the standard library, so obviously if there is something with tkinter, that would be an advantage. But it would be good if there is a way in tkinter that does not involve opening a new window (even if only temporarily). Commented Oct 3, 2020 at 22:04
  • @alani Nope, i dont think so, tkinter needs a window, but try my answer out? Commented Oct 3, 2020 at 22:09

3 Answers 3

2

A solution using xlib:

import Xlib.display
display = Xlib.display.Display()
screen = display.screen()
print(screen.height_in_pixels, screen.width_in_pixels)
Sign up to request clarification or add additional context in comments.

1 Comment

Nice to see how wonderful platform-independent is the Python! Thank you very much.
1

Try using tkinter, like:

from tkinter import *

root = Tk()
root.withdraw()

height,width = root.winfo_screenheight(),root.winfo_screenwidth()
print(height,width)

1 Comment

Thanks for posting this. Why the mainloop()?
0

The following link had a good answer https://www.blog.pythonlibrary.org/2015/08/18/getting-your-screen-resolution-with-python/

I had to modify the last line to make it run in Python 3.

import subprocess
cmd = ['xrandr']
cmd2 = ['grep', '*']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
p2 = subprocess.Popen(cmd2, stdin=p.stdout, stdout=subprocess.PIPE)
p.stdout.close()
resolution_string, junk = p2.communicate()
resolution = resolution_string.split()[0]
width, height = resolution.split(b'x')

1 Comment

If you are going to parse the result of an external command, I would be inclined to use xwininfo -root and look for the -geometry line (or indeed, the Width and Height)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.