Hello stdout world!...
Hello stdout world!...
The notion of attaching widgets to containers turns out to be at the core of layouts in
tkinter. Before we go into more detail on that topic, though, let’s get small.
Widget Resizing Revisited: Clipping
Earlier, we saw how to make widgets expand along with their parent window, by pass-
ing expand and fill options to the pack geometry manager. Now that we have a window
with more than one widget, I can let you in on one of the more useful secrets in the
packer. As a rule, widgets packed first are clipped last when a window is shrunk. That
is, the order in which you pack items determines which items will be cut out of the
display if it is made too small. Widgets packed later are cut out first. For example,
Figure 7-13 shows what happens when the gui4 window is shrunk interactively.
Figure 7-13. gui4 gets small
Try reordering the label and button lines in the script and see what happens when the
window shrinks; the first one packed is always the last to go away. For instance, if the
label is packed last, Figure 7-14 shows that it is clipped first, even though it is attached
to the top: side attachments and packing order both impact the overall layout, but only
packing order matters when windows shrink. Here are the changed lines:
Button(win, text='Hello', command=greeting).pack(side=LEFT)
Button(win, text='Quit', command=win.quit).pack(side=RIGHT)
Label(win, text='Hello container world').pack(side=TOP)
Figure 7-14. Label packed last, clipped first
tkinter keeps track of the packing order internally to make this work. Scripts can plan
ahead for shrinkage by calling pack methods of more important widgets first. For in-
stance, on the upcoming tkinter tour, we’ll meet code that builds menus and toolbars
at the top and bottom of the window; to make sure these are lost last as a window is
shrunk, they are packed first, before the application components in the middle.
396 | Chapter 7: Graphical User Interfaces