使用 tkinter canvas,要计算我显示的图形的大小,我通常使用该函数winfo_screenwidth()
,并相应地调整我的对象的大小。
但当在具有两个显示器的系统上使用时,winfo_screenwidth()
返回两个显示器的组合宽度——这会弄乱我的图形。
如何分别找出每个显示器的屏幕宽度(以像素为单位)?
我在各种 Linux 机器(Ubuntu 和 Mint)上使用多个版本的 Python 3.x 和多个版本的 tkinter(均为 8.5 或更高版本)时遇到过这个问题。
例如,第一个监视器的宽度为 1440 像素。第二个宽度为 1980 像素。winfo_screenwidth()
返回 3360。
我需要找到一种方法来独立确定每个显示器的屏幕宽度。
Thanks!
这是一个老问题,但仍然是:对于跨平台解决方案,您可以尝试屏幕信息模块,并通过以下方式获取有关每个监视器的信息:
import screeninfo
screeninfo.get_monitors()
如果您需要知道您的窗口之一位于哪台显示器上,您可以使用:
def get_monitor_from_coord(x, y):
monitors = screeninfo.get_monitors()
for m in reversed(monitors):
if m.x <= x <= m.width + m.x and m.y <= y <= m.height + m.y:
return m
return monitors[0]
# Get the screen which contains top
current_screen = get_monitor_from_coord(top.winfo_x(), top.winfo_y())
# Get the monitor's size
print current_screen.width, current_screen.height
(其中 top 是你的 Tk 根)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)