在Ubuntu上用Python获得监视器分辨率

2024-05-21 06:34:33 发布

您现在位置:Python中文网/ 问答频道 /正文

对于Ubuntu,win32api中是否有一个可用于GetSystemMetrics的可平等位代码?我需要得到显示器的宽度和高度,以像素为单位。


Tags: 代码宽度高度ubuntu单位像素显示器win32api
3条回答

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

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

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

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

3)可以使用subprocess python模块运行上述命令并提取信息

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

告诉我,如果这对你有帮助的话。

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

我认为你是一个GUI工具包。不然你为什么对屏幕尺寸感兴趣呢?

从PyGTK签出gtk.gdk.screen_width()gtk.gdk.screen_height()。QT也应该有类似的功能。

相关问题 更多 >