[ Team LiB ] Previous Section Next Section

Chapter 25. The Pack Geometry Manager

This chapter explores the pack geometry manager that positions widgets on the screen.

Geometry managers arrange widgets on the screen. This chapter describes the pack geometry manager, which is a constraint-based system. The next two chapters describe the grid and place geometry managers. The pack and grid geometry managers are quite general, while place is used for special-purpose applications. This book uses pack a lot because it was the original geometry manager for Tk. The grid geometry manager was added in Tk 4.1.

A geometry manager uses one widget as a parent, and it arranges multiple children (also called slaves) inside the parent. The parent is almost always a frame, but this is not strictly necessary. A widget can only be managed by one geometry manager at a time, but you can use different managers to control different widgets in your user interface. If a widget is not managed, then it doesn't appear on your display at all.

graphics/common_icon.gif

Don't pack and grid into the same manager widget.


For each individual manager widget — such as a frame, a labelframe, or a toplevel — you have the choice of using either pack or grid to manage all of its immediate children. Attempting to use both in the same manager results in an endless loop as both geometry managers try to control the window layout. This restriction applies only to the immediate children of a manager widget; you can use a different geometry manager for "descendents" that aren't immediate children. For example, you can choose to pack all of the immediate children of the . toplevel. Then, if one of the children of . is a frame, you can choose to use either pack or grid to manage the children of that frame.

The packer is a powerful constraint-based geometry manager. Instead of specifying in detail the placement of each window, the programmer defines some constraints about how windows should be positioned, and the packer works out the details. It is important to understand the algorithm the packer uses; otherwise, the constraint-based results may not be what you expect.

This chapter explores the packer through a series of examples. The background of the main window is set to black, and the other frames are given different colors so you can identify frames and observe the effect of the different packing parameters. When consecutive examples differ by a small amount, the added command or option is printed in bold courier to highlight the addition.

    [ Team LiB ] Previous Section Next Section