Navigation Logo 13.2  Slicing Navigation Logo

 

 

Added June 2005: Slicing is a nice concept for managing your widgets—especially when you know a user will resize them. However, it is just a concept and not something that is built into Tk. I have implemented some Java classes that do implement slicing. They are open source and I think you will find them easier to use than the layout managers in Swing's API.

The pack geometry manager is constraint based, which means that it decides itself where subwindows go and how big they will be and that the basis for its decisions are constraints it gets from the window manager, from the subwidgets being packed, and from you. Your constraints are specified when you invoke the pack configure action. When pack cannot satisfy all constraints, it makes its own decisions about what is important. These decisions always assigns greatest importance to overall geometry received from the window manager.

As mentioned in the previous section, widgets are placed under geometry management by pack this way:

pack SLAVES ?OPTIONS?

Sample invocations are
pack .frame1 -side top
pack .frame2 -side top
These two statements are equivalent to
pack .frame1 .frame2 -side top
or even to
pack .frame1 .frame2
because the default value for the -side option is "top."

As these examples imply, when multiple widgets are packed with one pack statement, the options apply to all of them.

A good source of building blocks for showing how pack works is the box procedure of Script ES12.4c. Figure 13.2a shows an example of its use. In that script, box is used three times to produce horizontal rectangles of varying colors. The widget name passed to box is the name of the parent widget – box itself names the frame widget it creates and returns that name. The third argument can be "h" or "v" for a horizontal or vertical rectangle. It can also be missing in which case box produces a square.

Although all subwindows go on a two-dimensional plane, we can organize them by thinking in one dimension at a time. The trick is to slice a window horizontally or vertically and then to slice each of the slices in the other direction as required. Slices of slices can be sliced, if necessary.

Again look at Figure 13.2a. This time, notice the horizontal slicing of the root window. Similarly, Figure 13.2b shows a vertical slicing. Using the "left" value for the -side option creates vertical slices from left to right.

Figure 13.2a: Horizontal slicing.
pack [box . white h]
pack [box . grey50 h]
pack [box . black h] 

Figure 13.2b: Vertical slicing.
pack [box . white v] -side left
pack [box . grey50 v] -side left
pack [box . black v] -side left 

Conceptually, the easiest way to create complicated windows is to use invisible frames when you want to reslice a horizontal slice vertically or vice versa. For example, to create the pattern in Figure 13.2c, the script can begin by creating horizontal slices as follows:

frame .top
frame .bottom
pack .top .bottom

After .top and .bottom are created, it is possible to create vertical slices within each of them. The pack geometry manager must consider these vertical slices to be slaves of .top and .bottom so we will name them as children of .top and .bottom.

For example, the two vertical slices in the .top frame could be named .top.left and .top.right. Or, they could be created using the box command using .top as the parent argument. It is this second approach that you see in the script of Figure 13.2c.

Figure 13.2c: Vertical slices within horizontal slices.
## create the widget for the top slice

frame .top

pack [box .top white] -side left

pack [box .top grey50] -side left ## create the widget for the bottom slice

frame .bottom

pack [box .bottom grey50] -side left

pack [box .bottom black] -side left ## pack the horizontal slices

pack .top .bottom

In Figure 13.2c you cannot see either the .top or the .bottom frame. Their only purpose is to provide a place where slicing can be done in the opposite direction.

Exercise 13.2a

Slice the root window vertically into three slices so that the left and right slices are white rectangles and the middle slice consists of a red square stacked on a blue square stacked on a yellow square.

Solution

Exercise 13.2b

Alter the previous exercise so that the middle blue square is sliced vertically into equal-sized black and blue rectangles. Do this by using box to create black and blue squares and then halving their widths. Do not look the original widths up in box's source code. Instead, obtain it from an existing box with the cget widget command action.

Solution

 

 

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