Python 3.x - toggling fullscreen in tkinter

孤者浪人 提交于 2020-01-14 02:58:07

问题


So far, I have a command that makes my window fullscreen. Now, predictably, I want to be able to exit fullscreen also.

This is the code I have:

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)

But the issue is that I can't get the window frame to reappear. I thought that doing win.overrideredirect(False) would work, but it didnt.


回答1:


not sure why it isn't working on your computer, but try this code sample:

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

the one thing i'm making sure i do is to store the geometry before going fullscreen, and then re applying it when i exit fullscreen. the global statement was needed because if i didn't use it the fullscreen function stored the geometry in a local variable instead of the one i created at the top.




回答2:


Change the overrideredirect flag before calling withdraw and deiconify.



来源:https://stackoverflow.com/questions/29986410/python-3-x-toggling-fullscreen-in-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!