Tkinter 网格几何管理器大小传播(带粘性) [英] Tkinter grid geometry manager size propagation (with sticky)

查看:24
本文介绍了Tkinter 网格几何管理器大小传播(带粘性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遗漏了一些关于大小如何在 Tk 中传播的信息.试试这个:

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 在 frame1 里面,label2 在 frame2 里面.label1 比 label2 窄,如白色背景所示.但是 frame1 和 frame2 的宽度相同,从它们的边框可以看出.我认为粘性会将 label1 扩展到与其父级相同的宽度.

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.

如果我把 label1 和 label2 放在同一个框架内,那么 label1 出来的宽度和 label2 一样:

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.

谢谢,丹

推荐答案

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

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)

有关 grid 的更多信息,以及 ruby​​、tcl、perl 和 python 中的示例,请参阅 tkdocs.com 上的网格页面

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

这篇关于Tkinter 网格几何管理器大小传播(带粘性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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