Search code examples
pythontkinter

Display fullscreen mode on Tkinter


How can I make a frame in Tkinter display in fullscreen mode? I saw this code, and it's very useful…:

>>> import Tkinter
>>> root = Tkinter.Tk()
>>> root.overrideredirect(True)
>>> root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))

…but is it possible to edit the code so that hitting Esc automatically makes the window "Restore down"?


Solution

  • This creates a fullscreen window. Pressing Escape resizes the window to '200x200+0+0' by default. If you move or resize the window, Escape toggles between the current geometry and the previous geometry.

    import Tkinter as tk
    
    class FullScreenApp(object):
        def __init__(self, master, **kwargs):
            self.master=master
            pad=3
            self._geom='200x200+0+0'
            master.geometry("{0}x{1}+0+0".format(
                master.winfo_screenwidth()-pad, master.winfo_screenheight()-pad))
            master.bind('<Escape>',self.toggle_geom)            
        def toggle_geom(self,event):
            geom=self.master.winfo_geometry()
            print(geom,self._geom)
            self.master.geometry(self._geom)
            self._geom=geom
    
    root=tk.Tk()
    app=FullScreenApp(root)
    root.mainloop()