tkinter winfo_screenwidth() 与双显示器一起使用时 [英] tkinter winfo_screenwidth() when used with dual monitors
问题描述
使用 tkinter 画布,为了计算我显示的图形的大小,我通常使用函数 winfo_screenwidth()
,并相应地调整我的对象的大小.但是当在有两个显示器的系统上使用时,winfo_screenwidth()
返回两个显示器的组合宽度——这会弄乱我的图形.如何分别找出每台显示器的屏幕宽度(以像素为单位)?
With tkinter canvas, to calculate the size of the graphics I display, I normally use the function winfo_screenwidth()
, and size my objects accordingly.
But when used on a system with two monitors, winfo_screenwidth()
returns the combined width of both monitors -- which messes up my graphics.
How can I find out the screen width in pixels of each monitor, separately?
我在各种 Linux 机器(Ubuntu 和 Mint)上使用多个版本的 Python 3.x 和多个版本的 tkinter(均为 8.5 或更高版本)时遇到了这个问题.
I have had this problem with several versions of Python 3.x and several versions of tkinter (all 8.5 or above) on a variety of Linux machines (Ubuntu and Mint).
例如,第一台显示器的宽度为 1440 像素.第二个是 1980 像素宽.wininfo_screenwidth()
返回 3360.
For example, the first monitor is 1440 pixels wide. The second is 1980 pixels wide. winfo_screenwidth()
returns 3360.
我需要找到一种方法来独立确定每个显示器的屏幕宽度.
I need to find a way to determine the screenwidth for each monitor independently.
谢谢!
推荐答案
这是一个老问题,但仍然:对于跨平台解决方案,您可以尝试 screeninfo 模块,并通过以下方式获取有关每个监视器的信息:
It is an old question, but still: for a cross-platform solution, you could try the screeninfo module, and get information about every monitor with:
import screeninfo
screeninfo.get_monitors()
如果您需要知道您的一个窗口位于哪个显示器上,您可以使用:
If you need to know on which monitor one of your windows is located, you could use:
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 根)
(where top is your Tk root)
这篇关于tkinter winfo_screenwidth() 与双显示器一起使用时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!