4.1 Widgets and Parcels

A geometry manager computes the size and location of widgets, that is the geometry, and displays the widgets on the screen. The geometry manager computes the geometry according to the widget hierarchy. During computation of the geometry, the manager takes the following three things into account:

  1. The geometry requested by slave widgets. Widgets like labels and messages request just enough space to displays their text or bitmap.

  2. The geometry requested by master widgets. Usually master widgets do not request an explicit geometry. But for example, if a frame widget is initialized with explicit values for width and height, the geometry manager takes these values into account.

  3. The options given to the geometry manager.

parcels

The geometry manager computes for each slave of a master widget a so-called parcel. The parcel is a rectangle and describes the space and the position computed for the slave. From the slaves' parcels the manager computes the parcel for the master. If the master does not request a specific geometry on its own, the manager will assign the master a parcel that encloses all slave parcels. Otherwise the geometry manager distributes the space in the parcel for the master to the slave parcels. This may shrink or grow the parcels for the slaves.

Options to the geometry manager affect usually the way how parcels are computed and how widgets are put into their parcels, if the parcels are larger (or smaller) than the parcel initially requested by the widget.

In the following we will show the two most important geometry managers which are provided in Tk. One is the packer, which can be used for simple arrangements, like placing several widgets in a row or in a line. The other geometry manager we will discuss is implemented by the grid command. As the name suggests, the grid command allows for arranging widgets in a grid-like fashion.

It is perfectly possible to mix geometry managers in a single toplevel widget provided that all slaves of a master are managed by the same manager. For example, suppose a toplevel widget that contains two frames which contain widgets themselves. Both frames must be managed by the same manager. The widgets in the frames can be managed by two different managers.

We discuss only the most important options these two managers provide, a complete description can be found in pack and grid. More information on the packer can also be found in John Ousterhout's book in Chapter 17.


Christian Schulte
Version 1.4.0 (20080702)