如何在不使用已弃用的 Gdk.Screen.get_width() 的情况下获取 Python / GTK 中的总屏幕大小?
How to get total screen size in Python / GTK without using deprecated Gdk.Screen.get_width()?
我想要整个桌面的 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 刚刚注:
Deprecated since version 3.22: Use per-monitor information instead
其他答案(如How do I get monitor resolution in Python or How to detect a computer's physical screen size in GTK) mention lots of other APIs and toolkits, or just give per-monitor information (like this for PyGtk),但我认为应该仍然可以(也是在将来)通过 PyGtk 获取整个桌面的尺寸。 (毕竟废弃的功能也依然提供了这个。)
我没有安装 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
还有这个:
基于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()))
我觉得这是最简单的说实话
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)
我想要整个桌面的 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 刚刚注:
Deprecated since version 3.22: Use per-monitor information instead
其他答案(如How do I get monitor resolution in Python or How to detect a computer's physical screen size in GTK) mention lots of other APIs and toolkits, or just give per-monitor information (like this for PyGtk),但我认为应该仍然可以(也是在将来)通过 PyGtk 获取整个桌面的尺寸。 (毕竟废弃的功能也依然提供了这个。)
我没有安装 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
还有这个:
基于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()))
我觉得这是最简单的说实话
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)