Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...