next up previous
Next: Using frames Up: The Tk Packer Previous: Some basics

Option-parameters

Parameters can be passed to pack-functions for changing the way widgets are packed. One example of this is the parameter '-side XXX', as shown in the examples above. This parameter will tell on which side of the remaining rectangle a smaller rectangle will contain the new widget. Possible sides are 'top', 'bottom', 'left' and 'right'.

Furthermore, a '-fill' parameter can be passed. This parameter tells the packer to expand the widget if pieces of the rectangle are not filled by the widget. Some examples:

Without any filling:

With '-fill x':

With '-fill y':

With '-fill both':

Another parameter is '-expand'. This parameter is similar to '-fill'. The difference between '-expand' en '-fill' becomes clear when some small wish-scripts are executed. Wish should be started anew for every script.

Script 1:

button .b1 -text hi
button .b2 -text helloworld
pack .b1 ; pack .b2

After the script is executed, there are two buttons in the wish-window. The upper button is a lot smaller than the lower one. Resizing the window has a limited effect: when the window is broadened, the buttons will shift to the middle of the window; when the window is heightened, the buttons will stay where they are.

Script 2:

button .b1 -text hi
button .b2 -text helloworld
pack .b1 -fill both ; pack .b2 -fill both

After the script has been executed, there are again two buttons in the wish-window, but this time, both buttons have the same size. The Packer has filled the upper button to contain the space that was left with script 1. Resizing the window has a somewhat different effect: when the window is broadened, the buttons will become as broad as the window; they will fill the available space in the rectangle. When the window is heightened, the buttons will not become any higher. This is because buttons are packed at the top by default and the areas containing the buttons will not become larger when the window is heightened; only the space that was left after the buttons were packed will become larger:

The strip of space left in the left part of the picture is not visible when the buttons are packed, but it is there.

Script 3:

button .b1 -text hi
button .b2 -text helloworld
pack .b1 -expand true -fill both ; pack .b2 -expand true -fill both

The buttons are of the same proportions as with script 2. When the window is resized now, the buttons will also grow in height. This is a result of the expand parameter: if the available space is enlarged (even if everything is already packed), widgets that are packed with '-expand true' will expand to contain the added space.

Script 4:

button .b1 -text hi
button .b2 -text helloworld
pack .b1 -expand true ; pack .b2 -expand true

The window now looks the same as after running script 1. Resizing the window has a peculiar effect: the buttons will end up in the middle of the available space. But because none of the buttons has been packed with a fill-parameter, the buttons are not enlarged. The difference between expand en fill should be clear now: 'expand' divides new available space between all widgets, 'fill' makes the widgets grow to fill the available space.



next up previous
Next: Using frames Up: The Tk Packer Previous: Some basics



Megens SA
Thu Jun 20 11:34:42 MET DST 1996