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.
All communication happens via textvariables and traces. I'm not sure whether this already counts as a minuscule Model / View / Controller example:
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 add variable $factor write {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
TV 2003-10-06: Coming across this page, I though it would be good to try this example block-wise, as it is a clear example of interaction programming, sort of classical from a past where I tried to find a widely portable and powerful means of doing this in roughly the time of th NeXT computer, knowing of course from 1978 onwards how I could program such a thing, even in assembly if I'd want to.
Many programmers and programming methods exhibit or breed no good interaction and decent programming awareness, and therefore I like any example like this, and thought I'd for the sake of argument, for actual use, I guess children can follow graphs so possibly for children, and for some fun make a bwise version:
newentry 60 30 {} {} 55.0 37.0 newentry 60 30 {} {} 55.0 103.0 newproc { set multiply.product [expr ${multiply.in1} * ${multiply.in2}] } multiply {in1 in2} product 40 {} {} 174.0 61.0 newmon 0 80 65 {} {} 290.0 61.0 connect wire0 Entry1 out multiply in1 connect wire1 Entry2 out multiply in2 connect wire2 multiply product Mon1 in
Just start some version of bwise (recent easy to use one: Bwise version 0.34, all in one file, 8.4 compatible), source the above script, and a graph with two input and an output text entry appears. Fill one number in, use the right (middle) mouse button to choose transfer from the popup menu, fill the other left (input) entry in with another number, and press return or choose 'run' or 'funprop' from the popup menu from the block to get the answer.
From that point on (can also be initialized though setting of pin variables or a script which calls the appropriate block based function), filling in a new value followed by return makes the network recompute...
Of course, it would be better to have error indication or a nice popup with 'please type a number here' added, and probably nicest would be a functional block fire-ing pull from the result block, possibly set of by a return or key press. Maybe sliders. Function analysis and exection pattern functions are in bwise, that's for next time.