What is the simplest way to get monitor resolution (preferably in a tuple)?
Another version using xrandr
:
import re
from subprocess import run, PIPE
output = run(['xrandr'], stdout=PIPE).stdout.decode()
result = re.search(r'current (\d+) x (\d+)', output)
width, height = map(int, result.groups()) if result else (800, 600)
If you're using wxWindows, you can simply do:
import wx
app = wx.App(False) # the wx.App object must be created first.
print(wx.GetDisplaySize()) # returns a tuple
In case you have PyQt4 installed, try the following code:
from PyQt4 import QtGui
import sys
MyApp = QtGui.QApplication(sys.argv)
V = MyApp.desktop().screenGeometry()
h = V.height()
w = V.width()
print("The screen resolution (width X height) is the following:")
print(str(w) + "X" + str(h))
For PyQt5, the following will work:
from PyQt5 import QtWidgets
import sys
MyApp = QtWidgets.QApplication(sys.argv)
V = MyApp.desktop().screenGeometry()
h = V.height()
w = V.width()
print("The screen resolution (width X height) is the following:")
print(str(w) + "X" + str(h))
You could use PyMouse. To get the screen size just use the screen_size()
attribute:
from pymouse import PyMouse
m = PyMouse()
a = m.screen_size()
a
will return a tuple, (X, Y)
, where X
is the horizontal position and Y
is the vertical position.
Link to function in documentation.
A cross platform and easy way to do this is by using TKinter that comes with nearly all the python versions so you don't have to install anything:
import tkinter
root = tkinter.Tk()
root.withdraw()
WIDTH, HEIGHT = root.winfo_screenwidth(), root.winfo_screenheight()
Try pyautogui:
import pyautogui
resolution = pyautogui.size()
print(resolution)