What controls automatic window resizing in Tkinter?

Tkinter top-level windows seem to have two β€œmodes”: where the size is determined by the application and where the user controls the size. Consider this code:

from tkinter import * class Test(Frame): def __init__(self,parent): Frame.__init__(self,parent) self.b1 = Button(self, text="Button 1",command=self.b1Press) self.b1.pack() def b1Press(self): print("b1Press") label = Label(self, text="Label") label.pack() root = Tk() ui = Test(root) ui.pack(fill='both', expand=1) root.mainloop() 

Each time I click the button, the visible window resizes to fit the extra label. However, if I manually resize the window (using the mouse), then it stops this automatic resizing behavior, and from then on I must manually resize the window to see new buttons as they are added.

What determines if the size of the top-level window is under the control of the application or user?

How can an application restore automatic calibration after manually resizing a user?

+8
python tkinter
source share
1 answer

The rule is quite simple - the top-level window has a fixed size when it is given a fixed size, otherwise it "shrinks to fit."

There are two ways to provide a fixed size top-level window: the user can manually resize it, or your application code can cause wm_geometry to give a size at startup.

To reset the original behavior, give the window zero geometry. For example:

 def __init__(self,parent): ... self.b2 = Button(self, text="Reset", command=self.b2Press) self.b2.pack() def b2Press(self): self.winfo_toplevel().wm_geometry("") 
+12
source share

All Articles