How do I get monitor resolution in Python?

后端 未结 30 1099
深忆病人
深忆病人 2020-11-22 13:49

What is the simplest way to get monitor resolution (preferably in a tuple)?

相关标签:
30条回答
  • 2020-11-22 14:39

    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)
    
    0 讨论(0)
  • 2020-11-22 14:42

    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
    
    0 讨论(0)
  • 2020-11-22 14:42

    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))
    
    0 讨论(0)
  • 2020-11-22 14:42

    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.

    0 讨论(0)
  • 2020-11-22 14:43

    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()
    
    0 讨论(0)
  • 2020-11-22 14:44

    Try pyautogui:

    import pyautogui
    resolution = pyautogui.size()
    print(resolution) 
    
    0 讨论(0)
提交回复
热议问题