[tkinter] widget size adjustment

Christian Gollwitzer auriocus at gmx.de
Wed Jun 22 17:47:30 EDT 2016


Am 22.06.16 um 23:18 schrieb Zachary Ware:
> On Wed, Jun 22, 2016 at 4:05 PM, Christian Gollwitzer <auriocus at gmx.de> wrote:
>> BTW, the Tkinter wrapper is a bit clumsy for this option. In the original
>> Tk, the sticky option is just a string. You can still pass that and do
>>
>>         sticky='nsew'
>>
>> instead of the clumsy
>>
>>         sticky=Tkinter.N+Tkinter.S+Tkinter.E+Tkinter.W
>
> There are constants in tkinter for this: NSEW, NS, NE, NW, SE, SW, and EW.

Yes, but this means that not only the combinations with three are 
missing (NEW = stretch left-right and align at the top etc.), but also 
you need to remember the exact order that the author of Tkinter has set.

I'm still thinking that passing a string is more in the spirit of Tk; do 
sticky='snew' or sticky='news' if you wish, which actually works. Plus 
you save the module prefix (unless you do "from tkinter import *"). I'm 
not getting the rationale behind these constants. Why should I do 
side=Tk.LEFT instead of side='left' ?

There are more points where Tkinter feels clunky compared to Tk in Tcl. 
For instance, gridding widgets with row and columnspan options can be 
done in Tcl in ASCII-art using -, x and ^ to indicate extensions:

grid .a   -
grid .b   .c
grid  ^   x

This defines a 3x2 grid where .a extends to the right (columnspan=2), .b 
extends downwards (rowspan =2), and the bottom right place is empty. The 
same thing in Python requires to do explicit row/column-counting

a.grid(row=0, column=0, columnspan=2)
b.grid(row=1, column=0, rowspan=2)
c.grid(row=1, column=1)


This gets increasingly uglier with the number of widgets in a frame.
If grid were a standalone function, as it is in Tk, it would not be 
difficult to provide something similar to the Tcl code. For whatever 
reason it was decided that it should be a method of the widgets to be 
placed and thus accepts only a single widget.

	Christian



More information about the Python-list mailing list