问题描述
我想要整个桌面的 X/Y 像素尺寸(可能跨越多个显示器),例如确定背景图像的最佳分辨率。
此 Python 代码仍然有效:
from gi import require_version
require_version("Gdk","3.0")
from gi.repository import Gdk
screen = Gdk.Screen.get_default()
print(screen.get_width()," ",screen.get_height())
但会打印弃用警告:
<string>:7: DeprecationWarning: Gdk.Screen.get_width is deprecated
<string>:7: DeprecationWarning: Gdk.Screen.get_height is deprecated
Gdk.Screen API docs 只是注意:
自 3.22 版起已弃用:改为使用每个显示器的信息
其他答案(如 How do I get monitor resolution in Python 或 How to detect a computer's physical screen size in GTK)提到了许多其他 API 和工具包,或者只是提供每个显示器的信息(如 this for PyGtk),但我认为它仍然应该是可能(将来也会)通过 PyGtk 获取整个桌面的尺寸。 (毕竟,弃用的功能也仍然提供这个。)
解决方法
基于the commit which removed those API calls,GDK用来计算屏幕尺寸的算法似乎基本上是这样的:
def get_screen_size(display):
mon_geoms = [
display.get_monitor(i).get_geometry()
for i in range(display.get_n_monitors())
]
x0 = min(r.x for r in mon_geoms)
y0 = min(r.y for r in mon_geoms)
x1 = max(r.x + r.width for r in mon_geoms)
y1 = max(r.y + r.height for r in mon_geoms)
return x1 - x0,y1 - y0
# example use
print(get_screen_size(Gdk.Display.get_default()))
,
我没有安装 GTK,所以很遗憾无法测试,但我认为这样的事情应该可行:
from gi.repository import Gdk
display = Gdk.Display.get_default()
monitor = display.get_primary_monitor()
scale_factor = monitor.get_scale_factor()
geometry = monitor.get_geometry()
x = geometry.x * scale_factor
y = geometry.y * scale_factor
print(f'Screen Size: {x}x{y}')
基于此: https://developer.gnome.org/gdk3/stable/GdkMonitor.html#gdk-monitor-get-scale-factor
这个:https://github.com/AdoptOpenJDK/openjdk-support/issues/172#issuecomment-497111004
还有这个:https://stackoverflow.com/a/63535459/7200940
,老实说,我认为这是最简单的
from gi import require_version
require_version("Gdk","3.0")
from gi.repository import Gdk
screen = Gdk.Display.get_default()
w=0
h=0
for x in range(0,screen.get_n_monitors()):
w += screen.get_monitor(x).get_geometry().width
if ( h < screen.get_monitor(x).get_geometry().height ):
h = screen.get_monitor(x).get_geometry().height
print (w,' ',h)