Navigation Logo 13.9  Solutions to Exercises Navigation Logo

 

 

Solution To Exercise 13.1a

proc forgetGeometry Master {
   foreach S [pack slaves $Master] { forgetGeometry $S }
   pack forget $Master
   foreach S [grid slaves $Master] { forgetGeometry $S }
   grid forget $Master
}

Solution To Exercise 13.2a

## left slice
  pack [box . white v] -side left
## middle slice
   pack [frame .middle] -side left
   pack [box .middle red] [box .middle blue] [box .middle yellow] 
## right slice
   pack [box . white v] -side left

Solution To Exercise 13.2b

## left slice
  pack [box . white v] -side left
## middle slice
   pack [frame .middle] -side left
   pack [box .middle red] [frame .middle.center] [box .middle yellow] 
## slice center frame of middle slice vertically
   set MiddleBlue [box .middle.center blue] 
   set Width [expr [$MiddleBlue cget -width]/2]
   $MiddleBlue configure -width $Width
   set MiddleBlack [box .middle.center black]
   $MiddleBlack configure -width $Width
   pack $MiddleBlack $MiddleBlue -side left
## right slice
   pack [box . white v] -side left

Solution To Exercise 13.3a

Here is one possibility.

pack [frame .f1] -side left -expand true -fill both
pack [box .f1 red]
pack [box .f1 green] -fill both
pack [frame .f2] -side left -expand true -fill both
pack [box .f2 blue] -expand true
pack [box .f2 yellow] -expand true -fill both

Solution To Exercise 13.3b

pack [box . red]  -side left
pack [frame .pad1] -side left -fill both -expand true
pack [box . blue] -side left
pack [frame .pad2] -side left -fill both -expand true
pack [box . black] -side left

Solution To Exercise 13.3c

pack [box . red] -expand true

Solution To Exercise 13.3d

## top slice
   pack [frame .top] -expand true -fill both
   pack [frame .top.white -bg white -width 3c -height 3c] \
        -side left -expand true -fill both 
   pack [frame .top.slider -bg grey50 -width 5m] \
        -side left -fill both
## bottom slice
   pack [frame .bottom] -fill both
   pack [frame .bottom.slider -bg grey50 -height 5m] \
        -side left -expand true -fill both
   pack [frame .bottom.pad -bg black -width 5m -height 5m] -side left

Solution To Exercise 13.5a

for {set I 0} {$I<=3} {incr I} {
   for {set J 0} {$J<=2} {incr J} {
       set Grid($I,$J) [box . black]
       grid $Grid($I,$J) -row $I -column $J -padx 2m -pady 2m
   }
}
destroy $Grid(2,1)
grid $Grid(1,1) -rowspan 2 -sticky news

Solution To Exercise 13.6a

Here is a completely commented rendition of the script.

# *********************************************************************
# the first slicing is horizontal, complete and has just one slice
# that lies across the entire top of the window
# ************
    pack [box . grey50]  -expand true
# *********************************************************************
# the second slicing is vertical, complete, and has just one slice
# that lies on the left under the top slice
# ************
   pack [box . grey50] -side left
# *********************************************************************
# the third slicing is horizontal, complete, and has one slice
# that lies just under first slice and to the left of the second
# ************
   pack [frame .fr] -side bottom -expand true -fill both
# *********************************************************************
# the fourth slicing is vertical, incomplete, and has two slices
# that lie just under the third slicing -- the attached cavity
# lies between the two slices
# ************
   pack [box . black] -side left 
   pack [box . white] -side right
# *********************************************************************
# the fifth slicing is a vertical slicing of the third slice; it is 
# incomplete, and has two slices; its cavity lies to the far left
# ************
   pack [box .fr black] -side right
   pack [box .fr white] -side right
From this rendition you see that

  1. The grey box on the left is centered in the second slice which extends up as far as the four squares up to the first slice.

  2. The upper row of two squares is the fifth slicing and has an attached cavity on the left.

  3. The lower row of two squares is the fourth slicing and has an attached cavity between the two squares.

  4. There are no other attached cavities.

  5. As the window expands horizontally:

    • No box will change size because there is no -fill option applied to a box.

    • The top grey box will remain centered horizontally in its slice that will continue to stretch across the top.

    • the two attached cavities will become visible. They will look like background space.

  6. As the window expands vertically:

    • No box will change size becaue there is no -fill option applied to a box.

    • The left grey box will remain centered vertically in its slice that lies on the left under the top slice.

    • The other five boxes appear in two horizontal and two vertical slices.

    • The boxes in horizontal slices will appear within extra vertical space because a -fill both option was used in the creation of these slices.

    • The boxes in the vertical slices will appear within extra vertical space because vertical slices expand vertically by default.

Figure 13.9a: Expansion of Figure 13.6a

Solution To Exercise 13.6b

## first vertical slice goes on left
   pack [frame .left] -side left -expand true -fill both
## slice the first slice horizontally
   pack [frame .left.white -bg white -width 3c -height 3c] \
        -expand true -fill both 
   pack [frame .left.slider -bg grey50 -height 5m] -fill both
## complete vertical slicing by horizontally slicing its attached cavity
   pack [frame .slider -bg grey50 -height 5m] \
        -expand true -fill both
   pack [frame .pad -bg black -width 5m -height 5m]

Solution To Exercise 13.7a

You can do this directly with frames or you can use the box procedure. If the latter, the need for a box to provide padding in the lower left can be a problem. What color should this box be? The same as the default background color, that is, [. cget -bg]. One way to get the default background color is, of course, to make the padding with a frame rather than a box. What dimensions should that frame have? The same dimensions as the adjoining boxes. Do not assume you know these dimensions. Get them with the cget action. If you do assume you know these dimensions, the code you write will break when somebody decides that the boxes should be a different size.

Here is a solution that uses box througout.

pack [frame .top] \
     -expand true -fill both
pack [box .top white v] \
     -side left
pack [box .top grey50] \
     -anchor ne -side left -expand true
pack [box . [. cget -bg]] \
     [box . black h] \
     -side left

Solution To Exercise 13.7b

grid [box . white v] [box . grey50] -sticky ne
grid x [box . black h]

Solution To Exercise 13.8a

## first horizontal slice has white boxes at either end
   pack [frame .top] -fill both
   pack [box .top white] -side left
   pack [box .top white] -side right
## third horizontal slice has white boxes at either end
   pack [frame .bottom] -fill both -side bottom
   pack [box .bottom white] -side left
   pack [box .bottom white] -side right
## second horizontal slice is just an expanding space
   pack [frame .space] -fill both -expand true

Solution To Exercise 13.8b

pack [frame .f -width 375 -height 300]
pack propagate .f false
pack [text .f.t]
 

 

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