简体   繁体   English

如何使用Tkinter在Python 2.7中创建等宽列

[英]How to create equal-width columns in Python 2.7 with Tkinter

How can I force the columns in a Tkinter application window to be of equal width? 如何强制Tkinter应用程序窗口中的列具有相等的宽度?

The tkdocs website states as follows: tkdocs网站的状态如下:

The width of each column (or height of each row) depends on the width or height of the widgets contained within the column or row. 每列的宽度(或每行的高度)取决于列或行中包含的小部件的宽度或高度。 This means when sketching out your user interface, and dividing it into rows and columns, you don't need to worry about each column or row being equal width [or height, presumably]. 这意味着在草绘用户界面并将其划分为行和列时,您不必担心每一列或每一行的宽度[或估计高度]相等。

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

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 (ie 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 (eg 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)? 另外,是否可以选择性地为网格中的某些列(但不是全部列)完成操作(例如,使X Y列的大小根据X Y列中最宽的窗口小部件来确定,而Z列的大小根据其中X列中最宽的窗口小工具进行调整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). 此配置与主窗口小部件相关联,而不与所包含的任何窗口小部件相关联(因为列当然可以包含许多窗口小部件)。

In standard Tk, this is done with: 在标准Tk中,这可以通过以下方式完成:

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

In Tkinter (note that uniform seems to be not documented in the docstring but it is exactly what you need): 在Tkinter中(请注意, uniform似乎没有记录在文档字符串中,但这正是您所需要的):

# "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.) (如您所见,这两种情况下的代码非常相似。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM