31

My understanding is that after initializing all frames and widgets in the __init__ method, the tkinter window resizes to fit all these components.

I would like to set the window's initialized size to be its minimum size. I want to be able to maximize and scale the window larger, but I never want the window to be small enough to start hiding widgets.

How do I accomplish this?

1
  • Are you saying to want to restrict the user from making the window smaller? Do you literally have so many widgets that some get chopped off, or is this a layout issue where things are getting chopped off perhaps incorrectly (ie: a scrollbar disappearing rather than a text widget shrinking)? Commented May 4, 2012 at 15:53

4 Answers 4

58

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.

1
  • 1
    This is much cleaner than my answer. Thanks! Commented May 4, 2012 at 19:33
30

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.

1
  • 1
    That's not true that you MUST put that code before your mainloop.
    – double_j
    Commented Apr 21, 2016 at 21:08
4

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()))
-5
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.

enter image description here

I hope you found this useful.

3
  • 3
    This doesn't seem to answer the question at all. What does your answer have to do with setting the minimum size of a window? Commented Aug 25, 2014 at 12:57
  • There is no single magic function to force a frame to a minimum or fixed size. However, you can certainly force the size of a frame by giving the frame a width and height.
    – miksiii
    Commented Aug 27, 2014 at 10:38
  • 2
    @miksiii This 'Magic' function is what the other answers are giving. This doesn't even prevent the user from resizing it in any way, and doesn't prevent widgets being hidden, as the OP asked.
    – Miriam
    Commented Dec 29, 2018 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.