Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 23.  The Pack Geometry Manager

Packing Order

The packer maintains an order among the children that are packed into a frame. By default, each new child is appended to the end of the packing order. The most obvious effect of the order is that the children first in the packing order are closest to the side they are packed against. You can control the packing order with the -before and -after packing options, and you can reorganize widgets after they have already been packed:

Example 23-17 Controlling the packing order.


# Create five labels in order
foreach label {one two three four five} {
   label .$label -text $label
   pack .$label -side left -padx 5
# ShuffleUp moves a widget to the beginning of the order
proc ShuffleUp { parent child } {
   set first [lindex [pack slaves $parent] 0]
   pack $child -in $parent -before $first
# ShuffleDown moves a widget to the end of the order
proc ShuffleDown { parent child } {
   pack $child -in $parent
ShuffleUp . .five
ShuffleDown . .three


The pack slaves command returns the list of children in their packing order. The ShuffleUp procedure uses this to find out the first child so that it can insert another child before it. The ShuffleDown procedure is simpler because the default is to append the child to the end of the packing order.

When a widget is repacked, then it retains all its packing parameters that have already been set. If you need to examine the current packing parameters for a widget, use the pack info command.

pack info .five
=> -in . -anchor center -expand 0 -fill none -ipadx 0 \
    -ipady 0 -padx 0 -pady 0 -side left

Pack the Scrollbar First


The packing order also determines what happens when the window is made too small. If the window is made small enough the packer will clip children that come later in the packing order. This is why, when you pack a scrollbar and a text widget into a frame, you should pack the scrollbar first. Otherwise, when the window is made smaller the text widget takes up all the space and the scrollbar is clipped.

      Previous section   Next section