Navigation Logo 13.7  Placing Widgets more Carefully Navigation Logo

 

 

For those cases in which a widget does not fill its associated slice, the pack geometry manager has a way of positioning widgets within slices. Similarly, the grid geometry manager has a way of positioning widgets within cells.

Most of the time, the default positioning is just what you want. With this positioning the widget is placed in the center of the slice or cell.

When the default absolutely will not do, you use a technique that depends on the geometry manager you are using.

Positioning Widgets within Slices

The -fill option actually has four possible values: "none" (the default), "both," "x," and "y." The first two are discussed above in Slicing for Size Changes. The -fill x option causes the widget to fill its slice in the horizontal directions but not in the vertical. The -fill y option causes the widget to fill its slice in the vertical directions but not in the horizontal.

The pack command also accepts an -anchor option that can be used to pull the widget to one side or corner without making it bigger. This option has nine possible values: "n," "e," "s," "w," "ne," "se," "sw," "nw," and "c." Except for the last one, all these values represent directions. Think of a slice as a map. The value of -anchor will determine the direction in which you will find the widget when the slice is larger than the widget. The c option is the default and it means the widget will be centered.

Figure 13.7a: Exercises 13.7a and 13.7b.

Exercise 13.7a

Reproduce Figure 13.7a with pack and by stretching the window with your mouse.

My solution uses five frames or boxes.

Solution

Positioning Widgets within Cells

Instead of the -fill and -anchor options, the grid geometry manager has -sticky. The value of this option is a string built up from four letters, "n," "e," "s," and "w," which again represent directions. A nonempty value of the -sticky option for a widget will cause that widget to stick to each of the indicated sides. If there is just one letter, L, then the effect is like -anchor L. If there are multiple letters, the widget will touch every cell side in every declared direction – stretching as necessary to do so. The value "news," therefore, makes the widget fill the entire cell.

Exercise 13.7b

Reproduce Figure 13.7a with grid and by stretching the window with your mouse.

Solution

 

 

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