Navigation Logo 12.8  Solutions to Exercises Navigation Logo

 

 

Solution To Exercise 12.1a

Making the focus stay at the entry widget is accomplished by disallowing it at the other places where it can be – namely the two buttons. Making the focus start at the entry widget is accomplished by using the focus command in the initializing script.

proc initForCaps {} {
   global switchState
   .edit_button configure -text Caps \
                -command { set Entry [string toupper $Entry] }
   set switchState initForSmall
}

proc initForSmall {} {
   global switchState
   .edit_button configure -text Small \
                -command { set Entry [string tolower $Entry] }
   set switchState initForCaps
}
proc quit {} {
   global Entry switchState
   destroy .entry .edit_button .quit_button
   unset Entry switchState
   bind . <Key-Tab> {}
}
entry .entry -textvariable Entry
pack .entry
button .edit_button -takefocus 0 -highlightthickness 0
pack .edit_button
button .quit_button -text Quit -command { quit } \
       -takefocus 0 -highlightthickness 0
pack .quit_button
initForCaps
bind . <Key-Tab> { $switchState; break }
focus .entry

Solution To Exercise 12.1b

pack [button .b1 -text Default]
pack [entry .f1]
pack [button .b2 -text {Takes Focus} -takefocus 1]
pack [entry .f2 -takefocus 1]
pack [button  .b3 -text {Does not Take Focus} -takefocus 0]
pack [entry .f3 -takefocus 0]

Solution To Exercise 12.3a

foreach W {3 6} {
   foreach R {raised sunken flat groove ridge} {
       pack  [frame .$R$W -width 4c -height 1c -relief $R -borderwidth $W]
   }
}     

Solution To Exercise 12.3b

The default borderwidth option is 0 which you can verify with

% frame .f
.f
% .f cget -borderwidth
0

Solution To Exercise 12.4a

pack [frame .f -width 1c -height 1c -background #ffffff]

Solution To Exercise 12.4b

foreach C {red yellow blue green magenta violet grey white black hokey} {
   if {[catch {frame .$C -width 2c -height 4m -background $C}]} {
      label .$C -text $C
   } 
   pack .$C
}

Solution To Exercise 12.4c

The solution to this exercise is also available as Script ES12.4c. It uses the array method of creating a regional variable, see above in Using Arrays for Missing Features.

proc box {Parent Color {Shape ""}} {
    ## create a new path name
       global box
       if {$Parent=="."} {set Parent ""}
       if {[info exists box(Number)]} { 
          incr box(Number) 
       } else { 
          set box(Number) 1 
       }
       set NewPath $Parent.$Color$box(Number)
    ## determine the height H and width W
       switch $Shape {
          "" { set H 1c; set W 1c }
          h  { set H 1c; set W 3c }
          v  { set H 3c; set W 1c }
          default { return -code error "$Shape is unknown shape for box" }
       }
    ## create the box
       return [frame $NewPath -bg $Color  -width $W -height $H]
    ## report the path name
       return $NewPath
}

Solution To Exercise 12.5a

proc font {Family Weight Slant Size} {
   ## Family: one of 
       set FAMILY {courier helvetica times}
   ## Weight: one of b n  (for bold and normal)
   ## Slant:  one of i n  (for italics/oblique and normal)
   ##         this gets translated differently for different families
   ## Size:   one of 
       set SIZE {8 10 11 12 14 17 24}
   ## returns a font name usable on multiple platforms

   if {[lsearch $FAMILY $Family ]<0} { 
      return -code 1 "1st arg to sdk_font should be one of $FAMILY"
   }
   if {$Weight=="b"} {
      set Weight bold
   } elseif {$Weight=="n"} { 
      set Weight medium
   } else {
      return -code 1 {2nd arg to sdk_font should be "n" or "b"}
   }
   if {$Slant=="i" } {
      if {$Family=="times" } {  set Slant i } { set Slant o }
   } elseif {$Slant=="n"} {
      set Slant r
   } else { 
      return -code 1 {3th arg to sdk_font should be "n" or "i"}
   }
   if {[lsearch $SIZE $Size]<0} { 
      return -code 1 "4th arg to sdk_font should be one of: $SIZE" 
   }
   return "-*-$Family-$Weight-$Slant-normal--$Size-*-*-*-*-*-*-*"
}

Solution To Exercise 12.6a

proc newCursor Widget {
  global newCursor
  incr newCursor(CursorIndex)
  if {$newCursor(CursorIndex) == $newCursor(NumberCursors) } { 
    set newCursor(CursorIndex) 0
  }
  set Cursor [lindex $newCursor(Cursors) $newCursor(CursorIndex)]
  puts $Cursor
  $Widget configure -cursor $Cursor
}

set newCursor(Cursors) { 
    X_cursor target plus tcross crosshair xterm circle man gumby mouse 
    spider spraycan pencil arrow center_ptr 
}
set newCursor(CursorIndex) 0
set newCursor(NumberCursors) [llength $newCursor(Cursors)]

pack [frame .fr -width 3c -height 3c -cursor X_cursor]

bind .fr <Button-1> "newCursor .fr"
 

 

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