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

匿名 (未验证) 提交于 2019-12-03 02:44:02

问题:

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

The tkdocs website states as follows:

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

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)?

回答1:

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:

# "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):

# "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.)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!