简体   繁体   English

如何使用 tkinter 在辅助显示器中全屏显示窗口?

[英]How to make a window fullscreen in a secondary display with tkinter?

I know how to make a window fullscreen in the "main" display, but even when moving my app's window to a secondary display connected to my PC, when I call:我知道如何在“主”显示器中全屏显示窗口,但即使将我的应用程序窗口移动到连接到我的 PC 的辅助显示器,当我调用时:

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

to fullscreen that window, it does so in the "main" display and not in the secondary one (the app's window disappears from the secondary display and instantly appears in the "main" one, in fullscreen).要全屏显示该窗口,它会在“主”显示而不是在次要显示中执行此操作(应用程序的窗口从辅助显示中消失,并立即全屏显示在“主”显示中)。

How can I make it fullscreen in the secondary display?如何在辅助显示器中使其全屏显示?

This works on Windows 7: If the second screen width and height are the same as the first one, you can use win1 or win2 geometry of the following code depending its relative position(leftof or rightof) to have a fullscreen in a secondary display:这适用于 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()

Try:尝试:

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"""

Windows, Python 3.8视窗,Python 3.8

In this solution, pressing F11 will make the window fullscreen on the current screen.在这个解决方案中,按F11将使窗口在当前屏幕上全屏显示。

Note that self.root.state("zoomed") is Windows specific according to doc.请注意,根据文档, self.root.state("zoomed")是 Windows 特定的。

self.root.overrideredirect(True) is weird in Windows and may have unwanted side effects. self.root.overrideredirect(True)在 Windows 中很奇怪,可能会产生不需要的副作用。 For instance I've had issues related to changing screen configuration with this option active.例如,在此选项处于活动状态时,我遇到了与更改屏幕配置相关的问题。

#!/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()

Super simple method working in 2021 2021年有效的超级简单方法

This works even if both displays are different resolutions.即使两个显示器的分辨率不同,这也有效。 Use geometry to offset the second display by the width of the first display.使用geometry将第二个显示器偏移第一个显示器的宽度。 The format of the geometry string is <width>x<height>+xoffset+yoffset : geometry字符串的格式为<width>x<height>+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)

As long as you know the width of the first display, this will work fine.只要您知道第一个显示器的宽度,就可以正常工作。 The X system TK runs on puts the second monitor to the right of the first one by default.默认情况下,运行的 X 系统 TK 将第二个监视器放在第一个监视器的右侧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM