Skip to main content
 首页 » 编程设计

Python 3.x之在 tkinter 中切换全屏

2024年10月25日12zlslch

到目前为止,我有一个命令可以让我的窗口全屏显示。现在,不出所料,我也希望能够退出全屏。

这是我的代码:

def toggFullscreen(self, win): 
 
    def exitFullscreen(event=None): 
        win.withdraw() 
        win.deiconify() 
        win.overrideredirect(False) 
        win.geometry('1024x700') 
 
    w = win.winfo_screenwidth() 
    h = win.winfo_screenheight() 
    win.overrideredirect(True) 
    win.geometry('%dx%d+0+0' % (w, h)) 
    win.focus_set() 
    win.bind('<Escape>', exitFullscreen) 

但问题是我无法让窗框重新出现。我认为执行 win.overrideredirect(False) 会起作用,但它没有。

请您参考如下方法:

不确定为什么它不能在您的计算机上运行,​​但请尝试以下代码示例:

#!python3 
 
import tkinter as tk 
 
geom="" 
 
def fullscreen(): 
    global geom 
    geom = root.geometry() 
    w = root.winfo_screenwidth() 
    h = root.winfo_screenheight() 
    root.overrideredirect(True) 
    root.geometry('%dx%d+0+0' % (w, h)) 
 
def exitfullscreen(): 
    global geom 
    root.overrideredirect(False) 
    root.geometry(geom) 
 
root = tk.Tk() 
tk.Button(root,text ="Fullscreen", command=fullscreen).pack() 
tk.Button(root,text ="Normal", command=exitfullscreen).pack() 
root.mainloop() 

我要确保做的一件事是在进入全屏之前存储几何图形,然后在退出全屏时重新应用它。全局语句是必需的,因为如果我不使用它,fullscreen 函数会将几何图形存储在局部变量中,而不是我在顶部创建的变量。