在多个 Tkinter LabelFrame 之间使用网格对齐小部件 [英] Aligning widgets using grid between multiple Tkinter LabelFrames

查看:32
本文介绍了在多个 Tkinter LabelFrame 之间使用网格对齐小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 Tkinter 布局,其中标签和输入字段在多个 LabelFrame 框之间垂直对齐.

I'm trying to create a Tkinter layout that has labels and entry fields vertically aligned across multiple LabelFrame boxes.

这是一些简化的代码:

#!/usr/bin/python
from Tkinter import *

win = Frame()
win.grid(sticky=N+S+E+W)

frame_a = LabelFrame(win, text='Top frame', padx=5, pady=5)
frame_b = LabelFrame(win, text='Bottom frame', padx=5, pady=5)
frame_a.grid(sticky=E+W)
frame_b.grid(sticky=E+W)

for frame in frame_a, frame_b:
    for col in 0, 1, 2:
        frame.columnconfigure(col, weight=1)

Label(win, text='Hi').grid(in_=frame_a, sticky=W)
Label(win, text='Longer label, shorter box').grid(in_=frame_b, sticky=W)

Entry(win).grid(in_=frame_a, row=0, column=1, sticky=W)
Entry(win, width=5).grid(in_=frame_b, row=0, column=1, sticky=W)

win.mainloop()

上面的代码产生一个如下所示的窗口:

The above code produces a window that looks like the below:

而我正在寻找某种对齐字段的方法,使窗口看起来更像这样(感谢 MS Paint):

Whereas I'm looking to find some way of aligning the fields so the window looks more like this (with thanks to MS Paint):

我在 grid() 中使用了 in_ 参数,但没有取得太大成果,我想不出还有什么可以试验的.

I've played around with in_ arguments to grid(), but not achieved very much, and I can't think of anything else to experiment with.

推荐答案

简短的回答是:你不能做你想做的事.grid 不跨多个容器管理其行和列.

The short answer is: you can't do what you want. grid does not manage its rows and columns across multiple containers.

但是,至少有几种方法可以达到您想要的效果.一种方法是为每个容器中的第一列指定一个明确的、相同的宽度.为此,您可以使用 grid_columnconfigure 方法为每列指定最小宽度.

However, there are at least a couple ways to achieve the effect you are wanting. One way is to give the first column in each container an explicit, identical width. To do that you can use the grid_columnconfigure method to give each column a minimum width.

另一种解决方案是给每个标签一个相同的宽度,这将有效地将第一列的宽度设置为相同(假设每个容器中的所有列都具有相同的权重).

Another solution is to give each label an identical width, which effectively will set the width of the first column to be the same (assuming all columns in each container have the same weight).

这篇关于在多个 Tkinter LabelFrame 之间使用网格对齐小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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