如何在 tkinter 中设置最小窗口大小? [英] How do I set a minimum window size in tkinter?

查看:57
本文介绍了如何在 tkinter 中设置最小窗口大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,在 __init__ 方法中初始化所有框架和小部件后,tkinter 窗口会调整大小以适应所有这些组件.

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.

我该如何实现?

推荐答案

你也可以在不进入主循环的情况下立即强制更新,方法如下:

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

在 effbot tkinterbook 上对 update() 的描述:

Description of update() at effbot tkinterbook:

处理所有挂起的事件,调用事件回调,完成任何待定几何管理,根据需要重绘小部件,并调用所有挂起的空闲任务.此方法应谨慎使用,因为它如果从错误的调用中调用,可能会导致非常糟糕的竞争条件地方(例如,从事件回调中,或从函数可以以任何方式从事件回调等中调用).当在有疑问,请改用 update_idletasks.

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.

这篇关于如何在 tkinter 中设置最小窗口大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆