48

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.

Bryan Oakley
  • 341,422
  • 46
  • 489
  • 636
Rasmus Damgaard Nielsen
  • 1,820
  • 4
  • 17
  • 33

7 Answers7

113

You can do it by calling

root.state('zoomed')
56

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)
A. Rodas
  • 19,561
  • 8
  • 63
  • 70
  • 2
    There is also `root.overrideredirect(True)` (removes title bar) – jfs Mar 18 '14 at 15:45
  • This works as long as I have one monitor. How could I change the code to maximize only over one monitor, not across whole virtual desktop? – graywolf Jul 10 '17 at 13:28
  • Excellent work, both solutions work depending on if you want title bar or not. – bretcj7 Sep 13 '17 at 02:34
  • 1
    This doesn't maximize the window as you can see on the three little buttons in the top right corner. Using the whole screen is different from maximizing a window. – buhtz Mar 29 '18 at 22:56
  • Setting a windows posiiton to 0|0 doesn't work on windows due to the title bar and other things with the window manager. @jfs But the ```Tk.overrideredirect``` function does not remove it directly. It rather (on windows) tells the windows window manager to ignore that window completely and for that reason the window doesn't get a frame nor title bar. – Nummer_42O Mar 15 '20 at 14:15
42

To show maximized window with title bar use the 'zoomed' attribute

root = Tk()
root.attributes('-zoomed', True)
azeeman
  • 421
  • 4
  • 3
12

I've found this on other website:

import Tkinter

MyRoot = Tkinter.Tk()
MyRoot.state("zoomed")

MyRoot.mainloop()

This solved my problem.

Sylvester Kruin
  • 2,114
  • 3
  • 8
  • 36
rpecas
  • 147
  • 1
  • 5
11

The first approach is to use the root.state('zoomed'), but is not supposed to be universally available. It works on Windows, and on my Ubuntu machine. However, under my Arch machine it doesn't.


The second is to first get the maxsize, and then set geometry manually, like:

m = root.maxsize()
root.geometry('{}x{}+0+0'.format(*m))

This works on most machines, but not on all. For example, under my Arch the maxsize() returns (1425, 870), while the real geometry of maximized window should be (1440, 848). So, you also couldn't rely on it.


And the third, in my opinion the best approach is to use root.wm_attributes('-zoomed', 1). It is universally available and seems to be the safest. On some machines in could zoom only by width or by height, but comparing to previous method, this one would never give you a window partly ouside of the screen.

Finally, if you want a fullscreen, not just zoomed window, use root.wm_attributes('-fullscreen', 1). It provides a native link to window manager's behavior, thus working much better, than playing with overrideredirect and setting geometry by hand (which on some platforms could lead to unmanaged window, which could be closed only by its own interface or killing the process, won't show on the taskbar, etc...)

thodnev
  • 1,484
  • 15
  • 19
2

The most pythonic is" root.wm_state('zoomed'), as mentioned by @J.F.Sebastian

splinter
  • 3,327
  • 6
  • 33
  • 74
0

With TkAgg as backend this is the only combination that maximized the window without fullscreen:

win_manager = plt.get_current_fig_manager()
win_manager.window.state('zoomed')
win_manager.full_screen_toggle()
twu
  • 11