Tkinter 网格几何管理器尺寸传播(带粘性)

发布于 2024-08-05 00:09:12 字数 958 浏览 6 评论 0原文

我遗漏了一些关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不念旧人 2024-08-12 00:09:12

行和列具有“权重”,描述它们如何增长或收缩以填充母版中的额外空间。默认情况下,行或列的权重为零,这意味着您已告诉标签填充该列,但尚未告诉该列填充主框架。

要解决此问题,请为该列指定权重。在这种特定情况下,任何正整数都可以:

frame1.columnconfigure(0, weight=1)
frame2.columnconfigure(0, weight=1)

有关 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:

frame1.columnconfigure(0, weight=1)
frame2.columnconfigure(0, weight=1)

For more info on grid, with examples in ruby, tcl, perl and python, see the grid page on tkdocs.com

若相惜即相离 2024-08-12 00:09:12

这种带有列和框架的解决方案是有效的,但要使标签在网格中具有相同的宽度,您不需要封闭的框架。请参阅下面的示例

from tkinter import *

root = Tk()

label1 = Label(root, text='short', bg='light green', relief=RIDGE)
label1.grid(sticky=E+W)
label2 = Label(root, text='quite a bit longer', bg='light green', relief=RIDGE)
label2.grid(sticky=E+W)

root.mainloop()

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

from tkinter import *

root = Tk()

label1 = Label(root, text='short', bg='light green', relief=RIDGE)
label1.grid(sticky=E+W)
label2 = Label(root, text='quite a bit longer', bg='light green', relief=RIDGE)
label2.grid(sticky=E+W)

root.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文