Python Tkinter grid-geometry sticky tuning not efficient (?)

I am developing a simple input dialog in Python / Tkinter using grid geometry and getting some unexpected behavior. When I start with this code:

 winAddNew = tk.Toplevel() winAddNew.title('Add New Customer') lblName = tk.Label(winAddNew, anchor=tk.W,font=fntNormal, text='Enter the complete name of the customer:') entName = tk.Entry(winAddNew, justify=tk.LEFT, font=fntNormal, width=20, textvariable=ctrlNewCustomerName) lblID = tk.Label(winAddNew, anchor=tk.W,font=fntNormal, text='Enter a unique three-character ID:') entID = tk.Entry(winAddNew, justify=tk.LEFT, font=fntNormal, width=8, textvariable=ctrlNewCustomerID) lblName.grid(row=0,column=0,columnspan=2,sticky=tk.W,padx=10,pady=(10,0)) entName.grid(row=1,column=0,columnspan=2,sticky=tk.W,padx=10,pady=(5,0)) lblID.grid(row=2,column=0,sticky=tk.W,padx=(10,0),pady=10) entID.grid(row=2,column=1,sticky=tk.W,padx=(0,10),pady=10) 

... this is what the rendered window looks like on my machine (Windows 7, Python 2.7.3). Note that the green lines are actually missing, I just use them to lay out what I think should occupy the positions of rows and columns.

enter image description here

So far so good. Now I want to set the width from entName to 60 to give a more prominent room for entering the client name. Since I have columnspan set to 2 , and because each widget is set with tk.W for the sticky parameter, I expect that increasing the width of entName will have no effect other than expanding the input widget itself and expanding the window, respectively. All other widget positions relative to each other should remain unchanged - right?

Nope. The entName also causes entName to be disconnected from lblID :

enter image description here

So what am I missing? Why entID stay where it was? And is there any way to make it stay in place, except block line 2 in its own frame?

+4
source share
1 answer

When you make the input widget larger, Tkinter must allocate extra space for column 0 or column 1. It does this according to the "weight" of each column.

Since you have not explicitly set the weight, all columns get a default value of zero. In your case, however, something should grow. Tkinter selects the first column if the columns are weightless. This explains why the entID is clicked - the first column expands to accommodate the widget widget.

If you give column 1 weight 1, any extra space will be allocated to that column, leaving column 0 wide enough to hold anything that restricts this column ( grid_columnconfigure .: any widgets that don't span the columns), you do this with the grid_columnconfigure .

For instance:

 winAddNew.grid_columnconfigure(1, weight=1) 

Note that weight can be any positive integer; he is an attitude. So, for example, if you set the column from 0 to 1 and columns 1 through 2, column 1 will be assigned twice as much extra space that is indicated in column 0.

Tkinter does not document this behavior well. The final documentation is on the Tcl / Tk manual pages. In particular, see Grid Algorithm on the grid page.

+7
source

All Articles