| 13.2 Slicing |
|
|
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 .frame1 -side top pack .frame2 -side topThese two statements are equivalent to pack .frame1 .frame2 -side topor even to pack .frame1 .frame2because the default value for the 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
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.
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.
|
Author's Home Page |
|
Order from Amazon. |