Prev: Changing icon on script
Next: some GPIB-tcl questions
From: shags72 on 1 Feb 2010 10:32 On Feb 1, 7:30 am, "nedbrek" <nedb...(a)yahoo.com> wrote: > Hello, > > "shags72" <je...(a)mtco.com> wrote in message > > news:9f6ce4ce-bb0b-4b23-8159-602fe99e8f39(a)b36g2000yqn.googlegroups.com... > > > On Jan 31, 9:45 am, Richard Owlett <rowl...(a)pcnetinc.com> wrote: > >> You might be interested in "A little multiplication toy" > >> athttp://wiki.tcl.tk/9219. IIRC it demonstrates much of what you > >> want. {For some reason I don't seem to be able to access the wiki > >> today.) > > > You are right that is about what I wanted! I tried to get it to work > > with my calculations but haven't had any luck yet. Here is my code and > > the original #ed out. The tk window comes up ok but it never comes out > > with the answer. I had all of the expr's together but wasn't sure if I > > had all of the [] in the right spots so I just went simple. Thanks in > > advance. > > > proc recompute {- - - -} { > > The script called by trace only needs three arguments, should be: > proc recompute {- - -} > > > global a b c d > > #catch {set c [expr {$a * $b}]} > > if {$c > 4} { > > set c 4 > > } > > set a [expr $a / 50] > > set b [expr $b / 12] > > set c [expr $c / 5] > > catch {set d [expr $a + $b]} > > catch {set d [expr $d - $c]} > > set d [format %1.0f $d] > > } > > All these reductions will keep triggering traces, and make all the variables > 0! Especially c <= 4, c / 5 (that's integer division {0..4}/5 is always 0). > > You should check for which variable {a b c} is being updated, and only > reduce that one, then recompute d (maybe use a different script for each > one). > > HTH, > Ned Ok,I think I got it. I didn't realize that if both values of a division were intergers the result would be an integer. Now I know that. Changed the divisor to a decimal and that changed the behavior. Then I don't really understand the trace but saw that it was now replacing the input with the answer from the division. So I set the vars to new vars as to not set off the trace and now it seems to be working. Let me know if this is considered bad code(as it probably is) please. Also, how can I change the size of the tk window and text size. Thanks a lot. proc main {} { global a b c d entry .a -width 5 -textvariable a label .cals -text cals entry .b -width 5 -textvariable b label .fat -text fat entry .c -width 5 -textvariable c label .pts -text pts label .= -text = label .d -width 10 -textvariable d eval pack [winfo children .] -side left foreach factor {a b c} { trace variable $factor w {recompute} } } proc recompute {- - -} { global a b c d #catch {set c [expr {$a * $b}]} if {$c > 4} { set cc 4 } else { set cc $c } set aa [expr $a / 50.] set bb [expr $b / 12.] set cc [expr $cc / 5.] catch {set d [expr $aa + $bb]} catch {set d [expr $d - $cc]} set d [format %1.0f $d] } main |