Raspbery Pi 4 上单独显示器上的两个全屏 tkinter 窗口

问题描述

我有一个简单的 TK 应用程序,它可以在一台显示器上全屏运行,但现在我想运行两个全屏窗口,在 RaspBerry Pi 4 上的每个显示器上运行一个。这两个显示器具有不同的分辨率并且可以单独正常工作,但我无法让两个全屏窗口工作,两个窗口都全屏显示在第一个显示器上。

我正在尝试使用 tkinter.Tk().geometry(),这是要走的路还是有更简单的方法

import tkinter

root = tkinter.Tk()

# specify resolutions of both windows
w0,h0 = 3840,2160
w1,h1 = 1920,1080

# set up window for display on HDMI 0 
win0 = tkinter.Toplevel()
win0.geometry(f"{w0}x{h0}")
win0.attributes("-fullscreen",True)
win0.config(cursor="none") # remove cursor 
win0.wm_attributes("-topmost",1) # make sure this window is on top 

# set up window for display on HDMI 1 
win1 = tkinter.Toplevel()
win1.geometry(f"{w1}x{h1}")
win1.attributes("-fullscreen",True)
win1.config(cursor="none")
win1.wm_attributes("-topmost",1)

解决方法

您必须将第二个窗口向右偏移第一个显示器的宽度(X 系统首先将显示器放在 HDMI0 端口上,然后将显示器从 HDMI1 放到正确)。geometry 可让您确保它们不重叠,然后 fullscreen 按预期工作。

geometry 字符串的格式为:<width>x<height>+xoffset+yoffset

root = tkinter.Tk()

# specify resolutions of both windows
w0,h0 = 3840,2160
w1,h1 = 1920,1080

# set up window for display on HDMI 0 
win0 = tkinter.Toplevel()
win0.geometry(f"{w0}x{h0}+0+0")
win0.attributes("-fullscreen",True)

# set up window for display on HDMI 1 
win1 = tkinter.Toplevel()
win1.geometry(f"{w1}x{h1}+{w0}+0") # <- the key is shifting right by w0 here 
win1.attributes("-fullscreen",True)

root.withdraw()  # hide the empty root window