Version 2 of A little multiplication toy

Updated 2003-06-27 15:02:29

if 0 {Richard Suchenwirth 2003-06-27 - As yet another attempt to teach programming to children, this tiny plaything (both in length of code and screen estate) does multiplication on the fly for the factors you enter into the two entries.

http://mini.net/files/multiply.jpg

All communication happens via textvariables and traces. I'm not sure whether this already counts as a minuscule Model / View / Controller example:

  • the variables a,b,c are the models
  • the label .c is a view on the result, c
  • the entries .a, .b are controllers for the user to change the model

But in any case, it's a pretty minimal example '(also for fancy widget names - I didn't believe they could be called .* or .= before I tried ...) }

 proc main {} {
    global a b c
    entry .a -width 5 -textvariable a
    label .* -text *
    entry .b -width 5 -textvariable b
    label .= -text =
    label .c -width 10 -textvariable c
    eval pack [winfo children .] -side left

    foreach factor {a b} {
        trace variable $factor w {recompute}
    }
 }

if 0 {In this proc which tries to recompute (that might fail if an entry has been cleared), we ignore the three arguments that a trace gets automatically added - we know what we want:}

 proc recompute {- - -} {
    global a b c
    catch {set c [expr {$a * $b}]}
 }

# Finally, let's go (it's a nice pattern to start with proc main, and end with calling it..)

 main

Arts and crafts of Tcl-Tk programming