How do I set a minimum window size in tkinter?

后端 未结 4 1067
情歌与酒
情歌与酒 2020-12-09 03:05

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

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 03:44

    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.

提交回复
热议问题