Search code examples
python-3.xtkinter

tkinter python maximize window


I want to initialize a window as maximized, but I can't find out how to do it. I'm using python 3.3 and Tkinter 8.6 on windows 7. I guess the answer is just here: http://www.tcl.tk/man/tcl/TkCmd/wm.htm#m8 but I have no idea how to input it into my python script

Besides, I need to get the width and height of the window (both as maximised and if the user re-scale it afterwards), but I guess I can just find that out myself.


Solution

  • If you want to set the fullscreen attribute to True, it is as easy as:

    root = Tk()
    root.attributes('-fullscreen', True)
    

    However, it doesn't show the title bar. If you want to keep it visible, you can resize the Tk element with the geometry() method:

    root = Tk()
    w, h = root.winfo_screenwidth(), root.winfo_screenheight()
    root.geometry("%dx%d+0+0" % (w, h))
    

    With winfo_width() and winfo_height() you can get the width and height or the window, and also you can bind an event handler to the <Configure> event:

    def resize(event):
        print("New size is: {}x{}".format(event.width, event.height))
    
    root.bind("<Configure>", resize)