Reputation: 791
Tkinter top level windows seem to have two "modes": where the size is being 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 press the button, the visible window changes size to fit an additional label. However, if I resize the window manually (with the mouse), then it stops this auto resizing behaviour, and from then on I have to manually change the size of the window to view new buttons as they are added.
What determines whether the size of a toplevel window is under control of the application or the user?
How can the application regain automatic sizing after the user has manually resized?
Upvotes: 8
Views: 4469
Reputation: 386105
The rule is pretty simple - a toplevel window has a fixed size whenever it has been given a fixed size, otherwise it "shrinks to fit".
There are two ways to give the top level window a fixed size: the user can resize it manually, or your application code can call wm_geometry
to give it a size at startup.
To reset the original behavior, give the window a null 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("")
Upvotes: 12