Posted to tcl by nscerqueira at Thu Mar 14 01:59:43 GMT 2013view raw
- proc dial {w x0 y0 x1 y1 args} {
- array set "" {-from 0 -to 100 -needle red
- -bg white -fg black}
- array set "" $args
- $w create oval $x0 $y0 $x1 $y1 -fill $(-bg)
- set xm [expr {($x0+$x1)/2.}]
- set ym [expr {($y0+$y1)/2.}]
- set id [$w create line $xm $ym $xm [+ $y0 10] -fill $(-needle) -width 3]
- $w create oval [- $xm 3] [- $ym 3] [+ $xm 3] [+ $ym 3] -fill $(-fg)
- set r [expr {$ym-$y0-15}]
- foreach i [dial'steps $(-from) $(-to)] {
- set a [dial'angle $(-from) $i $(-to)]
- set x [expr {$xm+$r*cos($a)}]
- set y [expr {$ym+$r*sin($a)}]
- $w create text $x $y -text $i -fill $(-fg)
- }
- trace var ::$(-variable) w [list dial'set $w $id $(-from) $(-to)]
-
-
-
- }
- foreach op {+ -} {
- proc $op {a b} "expr {\$a $op \$b}"
- }
- proc dial'steps {min max} {
- set step [expr {($max-$min)/50*10}]
- set res {}
- for {set i $min} {$i<=$max} {incr i $step} {
- lappend res $i
- }
- set res
- }
- proc dial'angle {min v max} {
- expr {(0.5 + double($v-$min)/($max-$min))*acos(-1)*1.5}
- }
- proc dial'set {w id min max var el op} {
- set v [uplevel 1 set $var]
- set v [expr {$v<$min? $min: $v>$max? $max: $v}]
- foreach {xm ym x1 y1} [$w coords $id] break
- set r [expr {hypot($y1-$ym,$x1-$xm)}]
- set a [dial'angle $min $v $max]
- set x1 [expr {$xm+$r*cos($a)}]
- set y1 [expr {$ym+$r*sin($a)}]
- $w coords $id $xm $ym $x1 $y1
- }
-
-
- proc Go {} {
- set msg "\"CPU usage\""
- set fin [open "|top -l 1 | grep $msg " r]
- set cpu [string trim [read $fin]]
- set cpuUser [string range [lindex $cpu 2] 0 [expr [string length [lindex $cpu 2]] -2]]
- set cpuSys [string range [lindex $cpu 4] 0 [expr [string length [lindex $cpu 4]] -2]]
- set cpuTotal [expr $cpuUser + $cpuSys]
- close $fin
- return $cpuTotal
- }
-
-
- #-------- test & demo - modify the value of two variables with <Up>/<Down> keys
- # catch {eval destroy [winfo children .]}
- # catch {unset V R}
-
- grid [ttk::button .b -text "Quit" -command {set cycle 1}] -row 2 -column 0 -sticky news
- grid [canvas .c -width 120 -height 120 ] -row 1 -column 0 -sticky news
- grid [ttk::label .t -text "Total CPU Usage"] -row 0 -column 0 -sticky news
-
- dial .c 10 10 110 110 -variable V -to 100 -bg yellow
- set V 0
- raise .
- update
-
- set cycle 0
- while {$cycle==0} {
- set V [Go]
- set color green
- if {$V>=50 } { set color yellow}
- if {$V>=80 } { set color red}
-
- .c configure -background $color
- update
- }
- exit
-