[Tutor] Config options on Tkinter Label Widget

Michael P. Reilly arcege@speakeasy.net
Thu, 5 Jul 2001 07:31:34 -0400 (EDT)


Rick Pasotto wrote
> Actually it *is* positioned to the left of the label. The problem is
> that the label is only as wide as the text. You need to add two things
> to the pack command: 'expand=1' to say you want the label to fill the
> available space, and 'fill=X' to say you want the label to expand
> horizontally.

FYI: `expand' does not "turn on and off" the `fill' option (you seem to
imply this).  They are not fully independant, but each works well without
the other.  Fill adjusts the widget's appearance and expand allows for
better resizing the widget.

Try the following:

>>> from Tkinter import *
>>> r1 = Tk()
>>> r1.title('expand=YES')
>>> l1 = Label(r1, text='hi', bg='green')
>>> l1.pack(expand=YES)
>>>
>>> r2 = Tk()
>>> r2.title('fill=BOTH')
>>> l2 = Label(r2, text='hi', bg='green')
>>> l2.pack(fill=BOTH)
>>>
>>> r3 = Tk()
>>> r3.title('fill=BOTH, expand=YES')
>>> l3 = Label(r3, text='hi', bg='green')
>>> l3.pack(fill=BOTH, expand=YES')
>>>
>>> r4 = Tk()
>>> r4.title('fill=BOTH, side=LEFT')
>>> l4 = Label(r4, text='hi', bg='green')
>>> l4.pack(fill=BOTH, side=LEFT)
>>>
>>> mainloop()

Now manually resize them all and see how the widget changes.  (Without
manually resizing them, it is hard to see how the geometry manager
is important.)

The 'expand=YES' widget stays centered in the window, but does not full
the window.  The 'fill=BOTH' widget fills the window, but does not expand
(vertically) because of the packing geometry (available, unallocated space
is below) and 'fill=BOTH, side=LEFT' widget does not expand horizontally
(available space is to the right).  And the 'fill=BOTH, expand=YES'
widget is centered, and uses all the space.

So, the expand option tells the geometry manager to use as much space
as possible, allowing for resizing.  The fill option tells the manager
to size the widget to the _given_ space in the directions given.

Hope this helps.

  -Arcege

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |