My understanding is that after initializing all frames and widgets in the __init__
method, the tkinter window resizes to fit all these components.
I wou
I figured it out:
def __init__(self, master):
...
self.master.update_idletasks()
self.master.after_idle(lambda: self.master.minsize(self.master.winfo_width(), self.master.winfo_height()))
root = Tk()
Since the root window is created. The root window is a main application window in our programs. It has a title bar and borders. These are provided by the window manager. It must be created before any other widgets.
root.geometry("250x150+300+300")
The geometry() method sets a size for the window and positions it on the screen. The first two parameters are width and height of the window. The last two parameters are x, y screen coordinates.
app = Example(root)
Here we create the instance of the application class.
root.mainloop()
Finally, we enter the mainloop. The event handling starts from this point. The mainloop receives events from the window system and dispatches them to the application widgets. It is terminated when we click on the close button of the titlebar or call the quit() method.
I hope you found this useful.
You can also force an update right away without entering your mainloop, by using something like this:
root = Tk()
# set up widgets here, do your grid/pack/place
# root.geometry() will return '1x1+0+0' here
root.update()
# now root.geometry() returns valid size/placement
root.minsize(root.winfo_width(), root.winfo_height())
Description of update() at effbot tkinterbook:
Processes all pending events, calls event callbacks, completes any pending geometry management, redraws widgets as necessary, and calls all pending idle tasks. This method should be used with care, since it may lead to really nasty race conditions if called from the wrong place (from within an event callback, for example, or from a function that can in any way be called from an event callback, etc.). When in doubt, use update_idletasks instead.
I've used this a good deal while messing about trying to figure out how to do things like get the size/position of widgets before jumping into the main loop.
I know this question is old, but here's another way:
root = Tk()
root.minsize(foo, bar)
root.minsize() sets the windows's minimum size to foo and bar, where foo and bar are the width and height of the window, respectively.
Your must, however put this code to run before your mainloop finished running. It will only take effect after the command is called.