Navigation Logo 13.3  Slicing for Size Changes Navigation Logo

 

 

Figure 13.3a shows what Figure 13.2a would look like after the user has resized the root window with the mouse. The window is larger. Although you cannot see it, the horizontal slices are wider. The boxes are the same size. This is default behavior. There are two options that change it.

Figure 13.3a: Figure 13.2a resized.

By default, horizontal slices grow or shrink horizontally to fit the space available to them. Vertical slices grow or shrink vertically. The widgets associated with them do not grow or shrink. If their slices become too small for them, they will be truncated. To make widgets grow with their slices, use -fill both. The middle slice in Figure 13.2b is packed the same way as in Figure 13.2a. The first slice is packed with -fill both. You can see the difference.

Figure 13.3b: Controlling widget resizing.
pack [box . white h]  -fill both
pack [box . grey50 h]
pack [box . black h] -expand true -fill both 

In Figure 13.3a, the space under the rectangles belongs to no slice. This is because, by default, horizontal slices do not expand vertically. To make horizontal or vertical slices expand in both directions, use -expand true. In Figure 13.3b, -expand true has been used with the black slice. So that the black frame will fill the entire expanded slice, the -fill both option is used as well.

In Figure 13.3b, the slices have expanded because the user resized the window. That is not the only way to make a slice larger than necessary to accommodate its associated widget. The same effect as you see in Figure 13.3b could be obtained by creating a larger black box while leaving the white and grey boxes the same. Then the window would be expanded to accommodate the black back and the slices for the white and grey boxes would need to expand accordingly. Because of the way it is packed, the white box would fill its slice.

Figure 13.3c is like Figure 13.3b but the middle slice is configured to expand vertically as well as horizontally. When multiple slices expand in the same space, pack allocates space between them in a way that is roughly proportional to their sizes.

Figure 13.3c: Using -fill options.
pack [box . white h]  -fill both
pack [box . grey50 h] -expand true
pack [box . black h] -expand true -fill both 

Summary

  • Widgets are associated with slices when they are packed.

  • Use -fill both to make a widget fill its slice at all times.

  • Vertical slices expand vertically. Horizontal slices expand horizontally. Use -expand true to make any slice expand in both directions.

  • Remember that invisible frames whose purpose is to contain other slicings are governed by the same principles. Use both -fill both and -expand true when packing them unless you have a good reason not to.

Exercise 13.3a

Make these two vertical slices:
pack [frame .f1] -side left
pack [frame .f2] -side left
Continue by slicing each vertical slice horizontally with two colored boxes. Try it a couple of different ways to see the effects of different uses of -fill both and -expand true on both the horizontal and the vertical slices.

Solution

Exercise 13.3b

Write a script that slices the root window into three colored boxes and two padding frames arranged in a horizontal line. The frames appear between the boxes and expand to keep the boxes equally apart from each other – you can give them an initial width or not as you like.

Solution

Exercise 13.3c

Pack a 1 centimeter square red box in such a way that it stays centered in the root window as the user expands and contracts it. So long as the root window is big enough, the square should stay the same size.

Solution

Figure 13.3d: Exercise 13.3d.

Exercise 13.3d

Write a script to produce the pattern of white, grey, and black frames shown in Figure 13.3d. The white frame represents a text area. The grey frames represent scrollbars. The white frame should expand in both directions with an enlarging window. The grey frames should expand only lengthwise and the black frame should remain the same size.

To achieve this affect you should begin with a horizontal or vertical slicing into two frames. I will describe the problems as if the first slicing were horizontal.

The top horizontal slice must expand in two directions. If it did not, you would be unable to get the white box to expand in two directions. As you pack the vertical slices within the top horizontal slice, remember you want them to expand in different ways.

The bottom horizontal slice must not expand in the vertical direction. This restriction prevents the boxes within it from expanding vertically – no matter how you pack them into their slices. The lower horizontal scrollbar must expand horizontally as far as the white box does. Make it expand as much as possible up to the fixed-size black box.

Solution

 

 

[Sample TK Application]
Author's Home Page
Navigation Logo [Book's Cover]
Order from Amazon.