我想要整个桌面的 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
Deprecated since version 3.22: Use per-monitor information instead
其他答案(如 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()))
关于python - 如何在不使用已弃用的 Gdk.Screen.get_width() 的情况下在 Python/GTK 中获取总屏幕大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65807310/