gpt4 book ai didi

python - 与双显示器一起使用时的 tkinter winfo_screenwidth()

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:55 38 4
gpt4 key购买 nike

使用 tkinter Canvas ,为了计算我显示的图形的大小,我通常使用函数 winfo_screenwidth(),并相应地调整我的对象的大小。但是当在具有两个显示器的系统上使用时,winfo_screenwidth() 返回两个显示器的组合宽度——这会弄乱我的图形。如何分别找出每台显示器的屏幕宽度(以像素为单位)?

我在各种 Linux 机器(Ubuntu 和 Mint)上的几个版本的 Python 3.x 和几个版本的 tkinter(所有 8.5 或更高版本)都遇到过这个问题。

例如,第一个监视器的宽度为 1440 像素。第二个是 1980 像素宽。 winfo_screenwidth() 返回 3360。

我需要找到一种方法来独立确定每台显示器的屏幕宽度。

谢谢!

最佳答案

这是一个老问题,但仍然是:对于跨平台解决方案,您可以尝试 screeninfo模块,并获取有关每个监视器的信息:

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 根目录)

关于python - 与双显示器一起使用时的 tkinter winfo_screenwidth(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30312875/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com