Version 4 of Click me

Updated 2007-10-03 08:38:06 by koen

Richard Suchenwirth 2003-02-18 - Reading a Java tutorial in a bored moment, I came upon the ClickMe example at http://java.sun.com/docs/books/tutorial/java/concepts/practical.html (2007-10-03: dead link), where with only two Java class files, in less than 40 LOC, you can implement a rectangle where a red spot appears when you click on it. After compilation, you have 5 files (one HTML) that perform this feat.

Hm, thought I, how would that look in Tcl?


 package require Tk
 proc drawSpot {w x y r} {
    $w delete spot
    $w create oval [expr {$x-$r}] [expr {$y-$r}] [expr {$x+$r}] [expr {$y+$r}]\
        -fill red -outline red -tag spot
 }
 pack [canvas .c]
 .c create rect 5 5 205 205 -fill white -outline black -tag box
 .c bind box <1> {drawSpot %W %x %y 7}

In order to reduce the ugly heap of expr calls, setok proposed this variation, with "specialist" functions for addition and subtraction:

 package require Tk
 proc + {x y} {expr {$x + $y}}
 proc - {x y} {expr {$x - $y}}
 proc drawSpot {w x y r} {
    $w delete spot
    $w create oval [- $x $r] [- $y $r] [+ $x $r] [+ $y $r]\
        -fill red -outline red -tag spot
 }
 pack [canvas .c]
 .c create rect 5 5 305 155 -fill white -outline black -tag box
 .c bind box <1> {drawSpot %W %x %y 7}

another variation without ugly expr calls:

 package require Tk
 interp alias {} = {} expr
 proc drawSpot {w x y r} {
    $w delete spot
    $w create oval [= $x-$r] [= $y-$r] [= $x+$r] [= $y+$r]\
      -fill red -outline red -tag spot
 }
 pack [canvas .c]
 .c create rect 5 5 305 155 -fill white -outline black -tag box
 .c bind box <1> {drawSpot %W %x %y 7}

(expr argument not in {...}, thus slower, but harmless here. Refer also to expr shorthand for Tcl9 -- RJM)


Arts and crafts of Tcl-Tk programming