如何阻止Tkinter Frame缩小以适合其内容? [英] How to stop Tkinter Frame from shrinking to fit its contents?

查看:213
本文介绍了如何阻止Tkinter Frame缩小以适合其内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是给我麻烦的代码.

This is the code that's giving me trouble.

f = Frame(root, width=1000, bg="blue")
f.pack(fill=X, expand=True)

l = Label(f, text="hi", width=10, bg="red", fg="white")
l.pack()

如果我用标签注释掉行,则框架显示为正确的宽度.但是,添加标签似乎将框架缩小到标签的大小.有办法防止这种情况发生吗?

If I comment out the lines with the Label, the Frame displays with the right width. However, adding the Label seems to shrink the Frame down to the Label's size. Is there a way to prevent that from happening?

推荐答案

默认情况下,packgrid 都缩小或增大窗口小部件以适合其内容 99.9%的时间.描述此功能的术语是几何传播".有一个命令可以在以下情况下打开或关闭几何传播使用pack(以及相似的 >).

By default, both pack and grid shrink or grow a widget to fit its contents, which is what you want 99.9% 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).

由于您使用的是pack,因此语法为:

Since you are using pack, the syntax would be:

f.pack_propagate(0)

root.pack_propagate(0),具体取决于您实际要影响的窗口小部件.但是,由于未指定框的高度,因此其默认高度为一像素,因此您仍然可能看不到内部小部件.要获得所需的全部效果,您需要给包含的框架一个宽度和一个高度.

or maybe root.pack_propagate(0), depending on which widgets you actually want to affect. However, because you haven't given the frame height, its default height is one 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.

话虽这么说,绝大多数时候您应该让Tkinter计算大小.当您关闭几何图形传播时,您的GUI不能很好地响应分辨率的变化,字体的变化等.Tkinter的几何图形管理器(packplacegrid)非常强大.学会利用这种力量.

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.

这篇关于如何阻止Tkinter Frame缩小以适合其内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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