How to stop Tkinter Frame from shrinking to fit its contents?

允我心安 提交于 2019-11-26 12:29:48

By default, tk frames shrink or grow to fit their contents, which is what you want 99% of the time. The term that describes this feature is "geometry propagation". There is a command to turn geometry propagation on or off when using pack (and a similar one for grid).

Since you are using pack, the syntax would be:

f.pack_propagate(0)

or maybe root.pack_propagate(0), depending on which widget(s) you actually want to affect. However, because you haven't given the frame height, it's default height is 1 pixel so you still may not see the interior widgets. To get the full effect of what you want, you need to give the containing frame both a width and a height.

That being said, the vast majority of the time you should let tkinter compute the size. When you turn geometry propagation off your GUI won't respond well to changes in resolution, changes in fonts, etc. tkinter's geometry managers (pack, place and grid) are remarkably powerful. Learn to take advantage of that power.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!