在Ubuntu上用Python获取Monitor分辨率

时间:2010-08-30 04:58:04

标签: python ubuntu monitor resolution

对于Ubuntu,win32api中的GetSystemMetrics是否有相同的代码?我需要以像素为单位获取显示器的宽度和高度。

5 个答案:

答案 0 :(得分:5)

我可以建议一些可以使用的方法。我没有使用xlib版本。

1)xlib(Python程序的X客户端库),如果在您的系统上可用。您可以查看“显示”方法和属性:python-xlib.sourceforge

2)在Ubuntu上,您可以执行以下操作来获得屏幕分辨率:

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

3)您可以使用子进程python模块,运行上述命令并提取信息

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

如果这对您有帮助,请告诉我。

答案 1 :(得分:5)

我假设你是一个GUI工具包。为什么你会对屏幕尺寸感兴趣?

从PyGTK查看gtk.gdk.screen_width()gtk.gdk.screen_height()。 QT应该有类似的东西。

答案 2 :(得分:1)

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())

答案 3 :(得分:0)

pyautogui简化了工作。

import pyautogui
pyautogui.size()

将您的屏幕分辨率作为包含(宽度,高度)

的元组获取

答案 4 :(得分:0)

只需使用Python 3进行测试-您可以使用Xlib模块:

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

主页:https://github.com/python-xlib/python-xlib


如果未安装Xlib模块,请运行

python3 -m pip install Xlib

sudo apt install python3-xlib