为什么 pack 和 grid 会覆盖父窗口小部件的预定义尺寸?
问题描述
我注意到每次创建 Tkinter 小部件并在小部件实例化期间或通过 稍后配置,一旦我添加 子窗口小部件 在我使用 网格布局管理器.
I noticed each time I create a Tkinter widget and set its dimensions either during the widget instantiation or by configuring them later, these last ones take no effect as soon as I add child widgets inside the parent one where I organize them using the grid layout manager.
我的问题:
我宁愿问我的问题只是为了确定我是一个初学者:这是否意味着使用网格布局管理器会覆盖预定义的父小部件的尺寸?可以解释一下这是如何工作的吗?
I rather ask my question just to be sure as I am a beginner: does this mean that using the grid layout manager overrides the predifined parent widget's dimensions ? May be some explanation on how this works ?
提前感谢您的澄清.
解决方案
grid 和 pack 都具有缩小以适应"行为——容器小部件(通常是框架或根窗口)的大小由孩子的大小.此功能称为几何传播.您可以关闭此功能,但 99% 的时间此功能将为您提供最佳效果.
Both grid and pack have a "shrink to fit" behavior -- the size of a container widget (typically a frame or the root window) is determined by the size of the children. This feature is called geometry propagation. You can turn this feature off, but 99% of the time this feature will give you the best results.
Tkinter 的理念是,您应该正确调整用户与之交互的小部件(Entry
、Button
等)的大小,然后让 pack
和 grid
确定框架和窗口的正确大小.
Tkinter's philosophy is that you should correctly size the widgets that the user interacts with (Entry
, Button
, etc), and then let pack
and grid
figure out the right size of the frames and windows.
Tkinter 在这方面做得非常好,根据我的经验,创建布局变得更加容易,因为您花费更少的时间来计算您需要多少空间,而将更多的时间集中在用户将实际与之交互的内容上.
Tkinter does a very good job of this, and in my experience makes creating layouts much easier because you spend less time trying to compute how much space you need and more time focusing on what the user will actually interact with.
相关文章