pack

The problem is how to display widgets. For example, there is an empty frame widget inside which a bunch of other widgets will be displayed. The pack geometry manager's solution to this problem is to successively pack widgets into the empty space left in the container widget. The container widget is the master widget, and the widgets packed into it are its slaves. The slaves are packed in a sequence: the packing order.

What the packer does is to take the next slave to be packed. It allocates an area for the slave to be packed into from the remaining space in the master. Which part of the space is allocated depends on instructions to the packer. When the size of the space has been determined, this is sliced off the free space, and allocated to the widget that is displayed in it. Then the remaining space is available to subsequent slaves.

At any one time the space left for packing is a rectangle. If the widget is too small to use up a whole slice from the length or breadth of the free rectangle, still a whole slice is allocated so that the free space is always rectangular.

It can be tricky to get the packing instructions right to get the desired finished effect, but a large number of arrangements of widgets is possible using the packer.

Let us take a simple example: three buttons packed into the root window. First we create the buttons; see also library('tcltk/examples/ex3.tcl'):

     button .b1 -text b1
     button .b2 -text b2
     button .b3 -text b3
     

then we can pack them thus:

     pack .b1 .b2 .b3
     

which produces a display of the three buttons, one on top of the other, button .b1 on the top, and button .b3 on the bottom.

images/tcltkex3.png

Three Plain Buttons
If we change the size of the text in button .b2 through the command:
     .b2 config -text "hello world"
     

then we see that the window grows to fit the middle button, but the other two buttons stay their original size.

images/tcltkex4.png

Middle Button Widens

The packer defaults to packing widgets in from the top of the master. Other directions can be specified. For example, the command:

     pack .b1 .b2 .b3 -side left
     

will pack starting at the left hand side of the window. The result of this is that the buttons are formed in a horizontal row with the wider button, .b2, in the middle.

images/tcltkex5.png

Packing From The Left