Getting Monitor resolution in Python on Ubuntu

柔情痞子 提交于 2021-02-07 19:36:21

问题


Is there a equatable bit of code to GetSystemMetrics in win32api, for Ubuntu? I need to get the monitors width and height in pixels.


回答1:


I assume you're a GUI toolkit. Why else would you be interested in the screen dimensions?

Check out gtk.gdk.screen_width() and gtk.gdk.screen_height() from PyGTK. Something similar should be available for QT.




回答2:


I can suggest a few approaches that can be used. I have not used the xlib version though.

1) xlib ( X client library for Python programs), if available on your system. You can look at "Display" methods and properties : python-xlib.sourceforge

2) On Ubuntu, you could do the following to get the screen resolution:

   xrandr  | grep \* | cut -d' ' -f4

3) You can use subprocess python module, to run the above command and extract the information

import subprocess
output = subprocess.Popen('xrandr | grep "\*" | cut -d" " -f4',shell=True, stdout=subprocess.PIPE).communicate()[0]
print output

Let me know, if this was helpful to you.




回答3:


import subprocess


def get_screen_resolution():
    output = subprocess.Popen('xrandr | grep "\*" | cut -d" " -f4',shell=True, stdout=subprocess.PIPE).communicate()[0]
    resolution = output.split()[0].split(b'x')
    return {'width': resolution[0], 'height': resolution[1]}

print(get_screen_resolution())



回答4:


I'm using python 3.5 on Ubuntu 16.04 LTS and had the same problem. I wanted to solve it without having to rely on extra modules because they aren't installed by default.

So I imagined this small function, which uses xrandr to get the data.

def monitorsInfo():

import subprocess

def getParenthesis(texte):
    content = None
    p1 = texte.find('(')
    if p1 >= 0:
        p2 = texte.find(')')
        if p2 > p1:
            content = texte[p1:p2+1]
    return content

commande = ['xrandr','--listmonitors']
res = subprocess.check_output(commande, shell=True).decode().split('\n')

monitors = {}

for l in res:
    if len(l) > 1:
        if l[0] != ' ':
            if l.split()[0] == l.split()[0].upper():

                options = getParenthesis(l)
                if options:
                    l = l.replace(options, '')
                z = l.split()

                # this is a connector
                name = z[0]
                conn = None
                primary = None
                geo = None
                size = None
                width = height = offsetx = offsety = None
                if z[1].lower() == 'connected':
                    conn = True
                    # monitor in use :-)
                else:
                    # screeen connection exists, no screen used
                    conn = False
                # for connected screens : get extra data
                if conn:
                    if z[2].lower() == 'primary':
                        primary = True
                        z.pop(2)
                    # other data for connected screeens
                    geo = z[2]   # get rid of extra 'primary'
                    size = ''.join(z[3:])
                    # get width and height
                    z = geo.split('+')
                    offsetx = int(z[1])
                    offsety = int(z[2])
                    z = z[0].split('x')
                    width = int(z[0])
                    height = int(z[1])


                # create a dict per monitor
                d = {}
                d['name'] = name
                d['connected'] = conn
                d['primary'] = primary
                d['geometry'] = geo
                d['options'] = options
                d['size'] = size
                d['width'] = width
                d['height'] = height
                d['offsetx'] = offsetx
                d['offsety'] = offsety

                monitors[name] = d

return monitors

The result is returned as a dictionary.




回答5:


pyautogui makes the work simple.

import pyautogui
pyautogui.size()

will fetch your screen resolution as a tuple containing (width, height)




回答6:


Just tested this with Python 3 - you can use the Xlib module:

from Xlib.display import Display
screen = Display(':0').screen()
print(screen.width_in_pixels, screen.height_in_pixels)

Homepage: https://github.com/python-xlib/python-xlib


If you don't have the Xlib module installed, run

python3 -m pip install Xlib

or

sudo apt install python3-xlib


来源:https://stackoverflow.com/questions/3597965/getting-monitor-resolution-in-python-on-ubuntu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!