如何使用 Tkinter 在 Python 2.7 中创建等宽列 [英] How to create equal-width columns in Python 2.7 with Tkinter

查看:66
本文介绍了如何使用 Tkinter 在 Python 2.7 中创建等宽列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何强制 Tkinter 应用程序窗口中的列等宽?

How can I force the columns in a Tkinter application window to be of equal width?

tkdocs 网站声明如下:

The tkdocs website states as follows:

每列的宽度(或每行的高度)取决于列或行中包含的小部件的宽度或高度.这意味着在勾勒出您的用户界面并将其分为行和列时,您无需担心每一列或每一行的宽度 [或高度,大概] 相等.

http://www.tkdocs.com/tutorial/grid.html

但我希望列的宽度相等,最好使所有列的宽度取决于任何列中最宽的小部件.有没有办法干净利落地实现这一点(即,在通过反复试验或任意为每列分配明显足够的最小宽度之前,不要使用单元格填充来使它们完全相同)?此外,是否可以有选择地为网格中的某些列而不是所有列执行此操作(例如,根据列 X 中最宽的小部件调整 X Y 列的大小?> Y,但 Z 列的大小取决于 Z 列中最宽的小部件)?

But I want the columns to be equal width, preferably by making the width of all columns depend upon the widest widget in any column. Is there a way of achieving this cleanly (i.e. not by playing around with cell padding until I get them all the same by trial and error or by arbitrarily assigning an apparently adequate minimum width to every column)? Also, can it be selectively done for some, but not all columns in a grid (e.g. so that columns X and Y are are sized according to the widest widget in column X or Y, but column Z is sized according to the widest widget in column Z)?

推荐答案

要使网格布局的所有列具有相同的宽度,您必须将这些列配置为具有相同的权重并具有相同的统一性团体.此配置与主小部件相关联,而不是任何包含的小部件(因为列当然可以包含许多小部件).

To make a gridded layout have all columns have the same width, you've got to configure those columns to have the same weight and to be in the same uniform group. This configuration is associated with the master widget, not any of the contained widgets (because columns can contain many widgets, of course).

在标准 Tk 中,这是通过以下方式完成的:

In standard Tk, this is done with:

# "fred" is just some arbitrary key; it means nothing other than to name the group
grid columnconfigure $master 0 -weight 1 -uniform fred

在 Tkinter 中(请注意,uniform 似乎是文档字符串中未记录的a> 但这正是您所需要的):

In Tkinter (note that uniform seems to be not documented in the docstring but it is exactly what you need):

# "fred" is just some arbitrary key; it means nothing other than to name the group
master.grid_columnconfigure(0, weight=1, uniform="fred")

然后对要设置的其他列索引重复此操作.(如您所见,这两种情况下的代码非常相似.)

Then repeat for the other column indices that you want to set things for. (As you can see, the code's very similar in these two cases.)

这篇关于如何使用 Tkinter 在 Python 2.7 中创建等宽列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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