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?
In Tkinter, minsize() method is used to set the minimum size of the Tkinter window. Using this method a user can set window's initialized size to its minimum size, and still be able to maximize and scale the window larger.
Tkinter windows can be resized automatically by hovering and pulling over the window. We can disable the resizable property using the resizable(boolean value) method. We will pass false value to this method which will disable the window to be resized.
The resizeTo() method resizes a window to a new width and height.
resizable() method is used to allow Tkinter root window to change it's size according to the users need as well we can prohibit resizing of the Tkinter window. So, basically, if user wants to create a fixed size window, this method can be used.
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("")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With