python - 如何在不使用已弃用的 Gdk.Screen.get_width() 的情况下在 Python/GTK 中获取总屏幕大小?

标签 python screen pygtk gdk

我想要整个桌面的 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 PythonHow 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/

相关文章:

python - Pandas 按范围分组,允许重复

android - OrientationEventListener (Tablet vs Mobile) 90度差异

c# - 触摸相对于屏幕的位置。 Windows手机

linux - GtkWidget.events 属性(例如)GtkTreeView 小部件的用途是什么?

python - 如何过滤或限制在 PyGTK 文本输入字段中输入的文本?

Python:从 URL 读取 HTML 源并将日期获取到程序中

python - 如何判断何时有多个索引匹配?

Java libgdx - 设置每个屏幕的方向

python - gtk:设置事件窗口

python - .kv 文件似乎不起作用