tkinter winfo_screenwidth() 在双显示器情况下的使用

9
使用tkinter画布时,我通常使用函数winfo_screenwidth()来计算我显示的图形的大小,并根据它的返回值调整我的对象大小。但在使用两个显示器的系统上,winfo_screenwidth()会返回两个监视器的组合宽度,这会破坏我的图形。
我曾在多个Linux机器(Ubuntu和Mint)上使用了几个版本的Python 3.x和tkinter(所有版本都是8.5或以上),都遇到过这个问题。
例如,第一个监视器宽度为1440像素,第二个为1980像素。 winfo_screenwidth() 返回 3360。
我需要找到一种方法来单独确定每个显示器的屏幕宽度,谢谢!

我认为这是此链接的副本。 - IronManMark20
@IronManMark20 不是的。它们是两个相关但不同的问题。 - Zizouz212
遇到了完全相同的问题,请参见: http://stackoverflow.com/questions/24377988/tkinter-on-ubuntu-14-02-reports-width-as-sum-of-both-monitors - Burtski
2个回答

8

这是一个老问题,但仍然有用:对于跨平台的解决方案,您可以尝试使用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 根窗口)

2

基于这个稍微不同的问题,我建议采用以下方法:

 t.state('zoomed')
 m_1_height= t.winfo_height()
 m_1_width= t.winfo_width() #this is the width you need for monitor 1 

这样窗口就会缩放以填满一个屏幕。另一台显示器的宽度只有 wininfo_screenwidth()-m_1_width

我还想指向一个优秀的 ctypes 方法,用于查找在 Windows 上的监视器尺寸,可以在这里找到:here。注意:与帖子所说不同,ctypes 在 stdlib 中!无需安装任何东西。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接