#!/usr/bin/wish # #NAME # pctbar # #SYNOPSIS # pctbar # #DESCRIPTION # Demo of widget that shows a percent-completion bar that # advances with time. # # A window is produced that displays a two-color bar that # is advanced from 0-100 to 100-0 at 1-second intervals. # After 100 seconds, a binding is added to the percent # entry widget, allowing you to type a number and press # return, to start the bar moving again. # #BUG # If you change the percent value while it is changing, the # behavior is instructive of how the after command works. # #AUTHOR John Chambers frame .tx -bd 5 -relief raised; pack .tx -side top -fill x frame .fr -bd 5 -relief raised; pack .fr -side top -fill both -expand 1 label .tx.desc -text {Demo of percent-completion bar} entry .tx.pval -textvariable pct -width 3 pack .tx.desc -in .tx -side left pack .tx.pval -in .tx -side left canvas .fr.pbar -bd 5 -relief ridge -height 15 -width 200 pack .fr.pbar -in .fr -fill both -expand 1 set b1 [.fr.pbar create rectangle 0 0 50 15 -fill green] set b2 [.fr.pbar create rectangle 50 15 100 15 -fill yellow] puts "b1: $b1" puts "b2: $b2" after 1000 Pct 0 proc Pct n1 { global b1 b2 pct if {$n1 > 100} { bind .tx.pval {Pct $pct} return } set ww [winfo width .fr.pbar] set pct $n1 puts "ww=\"$ww\" pct=$pct." set w1 [expr {($ww * $n1) / 100}] .fr.pbar coords $b1 0 0 $w1 15 .fr.pbar coords $b2 $w1 0 $ww 15 after 1000 Pct [incr n1] }