Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 23.  The Pack Geometry Manager

Choosing the Parent for Packing

In nearly all of the examples in this chapter, a widget is packed into its parent frame. In general, it is possible to pack a widget into any descendent of its parent. For example, the .a.b widget could be packed into .a, .a.c or .a.d.e.f. The -in packing option lets you specify an alternate packing parent. One motivation for this is that the frames introduced to get the arrangement right can cause cluttered names for important widgets. In Example 23-4 on page 335, the buttons have names like .one.alpha and .one.right.delta, which is not consistent. Here is an alternate implementation of the same example that simplifies the button names and gives the same result:

Example 23-18 Packing into other relatives.
# Create and pack two frames
frame .one -bg white
frame .two -width 100 -height 50 -bg grey50
# Create a row of buttons
foreach b {alpha beta} {
   button .$b -text $b
   pack .$b -in .one -side left
# Create a frame for two more buttons
frame .one.right
foreach b {delta epsilon} {
   button .$b -text $b
   pack .$b -in .one.right -side bottom
pack .one.right -side right
pack .one .two -side top

When you do this, remember that the order in which you create widgets is important. Create the frames first, then create the widgets. The stacking order for windows will cause the later windows to obscure the windows created first. The following is a common mistake because the frame obscures the button:

button .a -text hello
frame .b
pack .a -in .b

If you cannot avoid this problem scenario, then you can use the raise command to fix things up. Stacking order is also discussed on page 347.

raise .a

      Previous section   Next section