当小部件跨越多列时如何使 Tkinter 列具有相等的宽度(Python 2.7) [英] How to make Tkinter columns of equal width when widgets span multiple columns (Python 2.7)

查看:98
本文介绍了当小部件跨越多列时如何使 Tkinter 列具有相等的宽度(Python 2.7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面,标有ONE"、TWO"和THR"的按钮没有均匀分布.在我看来,问题的根源在于 Tk 假设任何包含跨越多列的小部件的一部分的列的默认最小宽度.但是,这种行为似乎没有记录,所以我不确定如何适应或调整它以使列具有相等的宽度 - 包括文本小部件跨越的两列和文本未跨越的单列小部件 - 从而均匀地间隔开按钮.我可以通过反复试验来拼凑它,即填充后一列直到与前两列匹配,但这似乎是一个糟糕的解决方案.

In the following, the buttons labelled 'ONE', 'TWO', and 'THR' do not get evenly spaced out. It seems to me that the root of the problem is that Tk is assuming a default minimum width for any column containing part of a widget that spans multiple columns. However, this behaviour appears to be undocumented, so I am unsure how to accommodate for or adjust it in order to get the columns to be of equal width - including the two columns spanned by the text widget and the single column not spanned by the text widget - and thus space out the buttons evenly. I could kludge it by trial and error, i.e. padding out the latter column until it matches the former two, but that seems a poor solution.

在下面与 @jwillis0720 的讨论之后,我添加了一个额外的列 (3) 和按钮 ('FIV') 以使问题更清晰.这个问题是关于当其中一些列被多列小部件跨越而其他列不是时如何使列具有相同的宽度.

Following discussion below with @jwillis0720, I've added an extra column (3) and button ('FIV') to make the problem clearer. This question is about how to get columns the same width when some of those columns are spanned by multi-column widgets and others are not.

import Tkinter

master = Tkinter.Tk()

Tkinter.Button(master, text='ONE').grid(row=0, column=0)
Tkinter.Button(master, text='TWO').grid(row=0, column=1)
Tkinter.Button(master, text='THR').grid(row=0, column=2)
Tkinter.Button(master, text='FOU').grid(row=1, column=2)
Tkinter.Button(master, text='FIV').grid(row=0, column=3) # added as per above edit
Tkinter.Text(master).grid(row=1, column=0, columnspan=2)

master.mainloop()

请注意,使用 grid_columnconfigureuniform 并不能解决这个问题.插入以下几行(请参阅此处对类似问题的回答:如何使用 Tkinter 在 Python 2.7 中创建等宽列) 只会使列变得有弹性;它们的大小仍然不均匀:

Please note that using grid_columnconfigure with uniform does not solve this problem. Inserting the following lines (see answer to similar question here: How to create equal-width columns in Python 2.7 with Tkinter) simply makes the columns stretchy; they remain unevenly sized:

master.grid_columnconfigure(0, weight=1, uniform='a')
master.grid_columnconfigure(1, weight=1, uniform='a')
master.grid_columnconfigure(2, weight=1, uniform='a')
master.grid_columnconfigure(3, weight=1, uniform='a') # added as per above edit

推荐答案

我认为您可能想要使用粘性选项.

I think you might want to use the sticky option.

sticky= 定义如果结果单元格是比小部件本身大.这可以是常数 S、N、E 和 W,或 NW、NE、SW 和 SE.

sticky= Defines how to expand the widget if the resulting cell is larger than the widget itself. This can be any combination of the constants S, N, E, and W, or NW, NE, SW, and SE.

例如,W (west) 表示小部件应该与左单元格边框.W+E 表示小部件应该被拉伸水平填充整个单元格.W+E+N+S 表示小部件应该向两个方向扩展.默认是将小部件居中在单元格中.

For example, W (west) means that the widget should be aligned to the left cell border. W+E means that the widget should be stretched horizontally to fill the whole cell. W+E+N+S means that the widget should be expanded in both directions. Default is to center the widget in the cell.

import Tkinter

master = Tkinter.Tk()

Tkinter.Button(master, text='ONE').grid(row=0, column=0, sticky='NW')
Tkinter.Button(master, text='TWO').grid(row=0, column=1, sticky='NW')
Tkinter.Button(master, text='THR').grid(row=0, column=2, sticky='NW')
Tkinter.Button(master, text='FOU').grid(row=1, column=2)
Tkinter.Text(master).grid(row=1, column=0, columnspan=2)


master.mainloop()

编辑

它看起来像什么.我的看起来像这样均匀间隔,除了文本小部件按指定占用两列.

Edit

What does it look like. Mine looks like this evenly spaced except the text widget takes up two columns as specified.

这篇关于当小部件跨越多列时如何使 Tkinter 列具有相等的宽度(Python 2.7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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