【问题标题】:Python 3.x - toggling fullscreen in tkinterPython 3.x - 在 tkinter 中切换全屏
【发布时间】:2015-05-02 02:19:49
【问题描述】:

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

这是我的代码:

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) 会起作用,但它没有。

【问题讨论】:

    标签: python tkinter toggle fullscreen


    【解决方案1】:

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

    #!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 函数会将几何图形存储在一个局部变量中,而不是我在顶部创建的那个。

    【讨论】:

    • 是一种有趣的方法,但对于我觉得应该相当简单的事情来说,它似乎有点复杂。
    【解决方案2】:

    在调用withdrawdeiconify 之前更改overrideredirect 标志

    【讨论】:

    • 这是与操作系统相关的行为吗?在我的 Windows 机器上,问题中的代码运行良好,即使我完全删除了 withdrawdeiconify
    • 这是有道理的。我目前正在工作,但一旦我尝试过就会标记为答案。
    猜你喜欢
    • 2023-03-11
    • 2018-08-25
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 2015-02-18
    • 1970-01-01
    相关资源
    最近更新 更多