【问题标题】:How to make a window fullscreen in a secondary display with tkinter?如何使用 tkinter 在辅助显示器中使窗口全屏显示?
【发布时间】:2014-12-04 20:52:54
【问题描述】:

我知道如何在“主”显示器中使窗口全屏显示,但即​​使将我的应用程序窗口移动到连接到我的 PC 的辅助显示器,当我打电话时:

self.master.attributes('-fullscreen', True)

要全屏显示该窗口,它会在“主”显示中而不是在辅助显示中(应用程序的窗口从辅助显示中消失,并立即出现在“主”显示中,全屏)。

如何在辅助显示器中使其全屏显示?

【问题讨论】:

  • Windows?还是别的什么?
  • 只是 Windows... Tk() 或 Toplevel()
  • 好吧,我想我的回答有点不清楚@TerryJanReedy。我想在 Windows SO 中全屏显示 Tk() 或 Toplevel() 窗口。
  • 对不起,我没有答案。

标签: python tkinter screen fullscreen


【解决方案1】:

这适用于 Windows 7:如果第二个屏幕的宽度和高度与第一个相同,您可以使用以下代码的 win1 或 win2 几何图形,具体取决于其相对位置(leftof 或 rightof)在辅助显示:

from Tkinter import *

def create_win():
    def close(): win1.destroy();win2.destroy()
    win1 = Toplevel()
    win1.geometry('%dx%d%+d+%d'%(sw,sh,-sw,0))
    Button(win1,text="Exit1",command=close).pack()
    win2 = Toplevel()
    win2.geometry('%dx%d%+d+%d'%(sw,sh,sw,0))
    Button(win2,text="Exit2",command=close).pack()

root=Tk()
sw,sh = root.winfo_screenwidth(),root.winfo_screenheight()
print "screen1:",sw,sh
w,h = 800,600 
a,b = (sw-w)/2,(sh-h)/2 

Button(root,text="Exit",command=lambda r=root:r.destroy()).pack()
Button(root,text="Create win2",command=create_win).pack()

root.geometry('%sx%s+%s+%s'%(w,h,a,b))
root.mainloop()

【讨论】:

  • 我有一个解决方法,但你应该得到“接受”并为此投赞成票,因为它可能对某人有用。谢谢!
【解决方案2】:

试试:

from Tkinter import *

rot = Tk()


wth,hgh = rot.winfo_screenwidth(),rot.winfo_screenheight()
#take desktop width and hight (pixel)
_w,_h = 800,600 #root width and hight
a,b = (wth-_w)/2,(hgh-_h)/2 #Put root to center of display(Margin_left,Margin_top)



def spann():
    def _exit():
        da.destroy()

    da = Toplevel()
    da.geometry('%dx%d+%d+%d' % (wth, hgh,0, 0))

    Button(da,text="Exit",command=_exit).pack()
    da.overrideredirect(1)
    da.focus_set()#Restricted access main menu




Button(rot,text="Exit",command=lambda rot=rot : rot.destroy()).pack()


but = Button(rot,text="Show SUB",command=spann)
but.pack()


rot.geometry('%sx%s+%s+%s'%(_w,_h,a,b))
rot.mainloop()
""" Geometry pattern 'WxH+a+b'
        W = Width
        H = Height
        a = Margin_left+Margin_Top"""

【讨论】:

    【解决方案3】:

    Windows、Python 3.8

    在此解决方案中,按F11 将使窗口在当前屏幕上全屏显示。

    请注意,self.root.state("zoomed") 根据文档是特定于 Windows 的。

    self.root.overrideredirect(True) 在 Windows 中很奇怪,可能会产生不必要的副作用。例如,在启用此选项的情况下,我遇到了与更改屏幕配置相关的问题。

    #!/usr/bin/env python3
    import tkinter as tk
    
    
    class Gui:
        fullScreen = False
    
        def __init__(self):
            self.root = tk.Tk()
            self.root.bind("<F11>", self.toggleFullScreen)
            self.root.bind("<Alt-Return>", self.toggleFullScreen)
            self.root.bind("<Control-w>", self.quit)
            self.root.mainloop()
    
        def toggleFullScreen(self, event):
            if self.fullScreen:
                self.deactivateFullscreen()
            else:
                self.activateFullscreen()
    
        def activateFullscreen(self):
            self.fullScreen = True
    
            # Store geometry for reset
            self.geometry = self.root.geometry()
    
            # Hides borders and make truly fullscreen
            self.root.overrideredirect(True)
    
            # Maximize window (Windows only). Optionally set screen geometry if you have it
            self.root.state("zoomed")
    
        def deactivateFullscreen(self):
            self.fullScreen = False
            self.root.state("normal")
            self.root.geometry(self.geometry)
            self.root.overrideredirect(False)
    
        def quit(self, event=None):
            print("quiting...", event)
            self.root.quit()
    
    
    if __name__ == '__main__':
        Gui()
    

    【讨论】:

    • 这似乎不起作用。窗口移除了装饰并获得了屏幕的大小,但它没有居中。全屏窗口的位置将与开始的小窗口相同(左上角将保持相同的坐标)。
    【解决方案4】:

    2021年工作的超级简单方法

    即使两个显示器的分辨率不同,这也有效。使用geometry 将第二个显示偏移第一个显示的宽度。 geometry 字符串的格式为&lt;width&gt;x&lt;height&gt;+xoffset+yoffset

    root = tkinter.Tk()
    
    # specify resolutions of both windows
    w0, h0 = 3840, 2160
    w1, h1 = 1920, 1080
    
    # set up a window for first display, if wanted  
    win0 = tkinter.Toplevel()
    win0.geometry(f"{w0}x{h0}+0+0")
    
    # set up window for second display with fullscreen 
    win1 = tkinter.Toplevel()
    win1.geometry(f"{w1}x{h1}+{w0}+0") # <- this is the key, offset to the right by w0
    win1.attributes("-fullscreen", True)
    
    

    只要您知道第一个显示的宽度,就可以正常工作。 TK 运行的 X 系统默认将第二个监视器放在第一个监视器的右侧。

    【讨论】:

    • 强制将其移至另一个窗口有效,但设置全屏属性使其再次在主监视器上全屏显示。