To make all of these grow along with their window, though, we also need to make the
container frame expandable; widgets expand beyond their initial packer arrangement
only if all of their parents expand, too. Here are the changes in gui4.py:
win = Frame()
win.pack(side=TOP, expand=YES, fill=BOTH)
Button(win, text='Hello', command=greeting).pack(side=LEFT, fill=Y)
Label(win, text='Hello container world').pack(side=TOP)
Button(win, text='Quit', command=win.quit).pack(side=RIGHT, expand=YES,fill=X)
When this code runs, the Frame is assigned the entire top side of its parent as before
(that is, the top parcel of the root window); but because it is now marked to expand
into unused space in its parent and to fill that space both ways, it and all of its attached
children expand along with the window. Figure 7-17 shows how.
Figure 7-17. gui4 gets big with an expandable frame
Using Anchor to Position Instead of Stretch
And as if that isn’t flexible enough, the packer also allows widgets to be positioned
within their allocated space with an anchor option, instead of filling that space with a
fill. The anchor option accepts tkinter constants identifying all eight points of the
compass (N, NE, NW, S, etc.) and CENTER as its value (e.g., anchor=NW). It instructs the packer
to position the widget at the desired position within its allocated space, if the space
allocated for the widget is larger than the space needed to display the widget.
Figure 7-16. Packing with expand and fill options
Adding Multiple Widgets| 399