expand-ing

The fill packing option specifies whether the widget should fill space left over in its slice of space. A further option to take into account is what happens when the space allocated to the master widget is much greater than the that used by its slaves. This is not usually a problem initially because the master container widget is sized to shrink-wrap around the space used by its slaves. If the container is subsequently resized, however, to a much larger size there is a question as to what should happen to the slave widgets. A common example of resizing a container widget is the resizing of a top-level window widget.

The default behavior of the packer is not to change the size or arrangement of the slave widgets. There is an option though through the expand option to cause the slices of space allocated to slaves to expand to fill the newly available space in the master. expand can have one of two values: 0 for no expansion, and 1 for expansion.

Take the listbox-scrollbar example; see also library('tcltk/examples/ex10.tcl'):

     listbox .l
     scrollbar .s
     pack .l -side left
     pack .s -side left -fill y

Initially this looks good, but now resize the window to a much bigger size. You will find that the listbox stays the same size and that empty space appears at the top and bottom of it, and that the scrollbar resizes in the vertical. It is now not so nice.


images/tcltkex10.png
Scrollbar And Listbox, Problems With Resizing

We can fix part of the problem by having the listbox expand to fill the extra space generated by resizing the window.

     pack .l -side left -expand 1

images/tcltkex10a.png
Scrollbar And Listbox, Almost There

The problem now is that expand just expands the space allocated to the listbox, it doesn't stretch the listbox itself. To achieve that we need to apply the fill option to the listbox too.

     pack .l -side left -expand 1 -fill both

images/tcltkex10b.png
Scrollbar And Listbox, Problem Solved Using fill

Now whichever way the top-level window is resized, the listbox-scrollbar combination should look good.

If more than one widget has the expansion bit set, then the space is allocated equally to those widgets. This can be used, for example, to make a row of buttons of equal size that resize to fill the widget of their container. Try the following code; see also library('tcltk/examples/ex11.tcl'):

     button .b1 -text "one"
     button .b2 -text "two"
     button .b3 -text "three"
     pack .b1 .b2 .b3 -side left -fill x -expand 1

images/tcltkex11.png
Resizing Evenly Sized Widgets

Now resize the window. You will see that the buttons resize to fill the width of the window, each taking an equal third of the width.

Please note: the best way to get the hang of the packer is to play with it. Often the results are not what you expect, especially when it comes to fill and expand options. When you have created a display that looks pleasing, always try resizing the window to see if it still looks pleasing, or whether some of your fill and expand options need revising.