Tkinter 网格几何管理器尺寸传播(带粘性)
我遗漏了一些关于 Tk 中大小如何传播的信息。试试这个:
from Tkinter import *
root = Tk()
frame1 = Frame(root, border=4, relief=RIDGE)
frame1.grid(sticky=E+W)
frame2 = Frame(root, border=4, relief=RIDGE)
frame2.grid(sticky=E+W)
label1 = Label(frame1, text='short', background='white')
label1.grid(sticky=E+W)
label2 = Label(frame2, text='quite a bit longer', background='white')
label2.grid(sticky=E+W)
root.mainloop()
label1 在frame1 内部,label2 在frame2 内部。 从白色背景可以看出,label1 比 label2 窄。但从边框来看,frame1 和frame2 的宽度相同。我认为粘性会将 label1 扩展到与其父级相同的宽度。
如果我将 label1 和 label2 放在同一帧内,则 label1 的宽度与 label2 的宽度一样:
frame1 = Frame(root, border=4, relief=RIDGE)
frame1.grid(sticky=E+W)
label1 = Label(frame1, text='short', background='white')
label1.grid(sticky=E+W)
label2 = Label(frame1, text='quite a bit longer', background='white')
label2.grid(sticky=E+W)
我错过了什么?在现实生活中,我有一些堆叠的嵌套框架,它们没有按照我想要的方式扩展。
谢谢, 担
I'm missing something about how sizes propagate in Tk. Try this:
from Tkinter import *
root = Tk()
frame1 = Frame(root, border=4, relief=RIDGE)
frame1.grid(sticky=E+W)
frame2 = Frame(root, border=4, relief=RIDGE)
frame2.grid(sticky=E+W)
label1 = Label(frame1, text='short', background='white')
label1.grid(sticky=E+W)
label2 = Label(frame2, text='quite a bit longer', background='white')
label2.grid(sticky=E+W)
root.mainloop()
label1 is inside frame1, and label2 is inside frame2.
label1 comes out narrower than label2, as seen by the white background. But frame1 and frame2 are the same width, as seen by their borders. I thought the stickiness would expand label1 to be the same width as its parent.
If I put label1 and label2 inside the same frame, then label1 comes out as wide as label2:
frame1 = Frame(root, border=4, relief=RIDGE)
frame1.grid(sticky=E+W)
label1 = Label(frame1, text='short', background='white')
label1.grid(sticky=E+W)
label2 = Label(frame1, text='quite a bit longer', background='white')
label2.grid(sticky=E+W)
What am I missing? In real life, I have some stacked nested frames that are not expanding as I would like.
Thanks,
Dan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
行和列具有“权重”,描述它们如何增长或收缩以填充母版中的额外空间。默认情况下,行或列的权重为零,这意味着您已告诉标签填充该列,但尚未告诉该列填充主框架。
要解决此问题,请为该列指定权重。在这种特定情况下,任何正整数都可以:
有关
grid
的更多信息,以及 ruby、tcl、perl 和 python 中的示例,请参阅 tkdocs.com 上的网格页面Rows and columns have "weight" which describes how they grow or shrink to fill extra space in the master. By default a row or column has a weight of zero, which means you've told the label to fill the column but you haven't told the column to fill the master frame.
To fix this, give the column a weight. Any positive integer will do in this specific case:
For more info on
grid
, with examples in ruby, tcl, perl and python, see the grid page on tkdocs.com这种带有列和框架的解决方案是有效的,但要使标签在网格中具有相同的宽度,您不需要封闭的框架。请参阅下面的示例
This solution with columns and frames works, but to get labels to have the same width in a grid, you do not need the enclosing frames. See below example