summaryrefslogtreecommitdiffstats
path: root/library/demos/timer
blob: b2edd114786ae9ef7f38f4050cc750a08d05455b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/sh
# the next line restarts using wish \
exec wish "$0" "$@"

# timer --
# This script generates a counter with start and stop buttons.
#
# SCCS: @(#) timer 1.6 96/02/16 10:49:20

label .counter -text 0.00 -relief raised -width 10
button .start -text Start -command {
    if $stopped {
	set stopped 0
	tick
    }
}
button .stop -text Stop -command {set stopped 1}
pack .counter -side bottom -fill both
pack .start -side left -fill both -expand yes
pack .stop -side right -fill both -expand yes

set seconds 0
set hundredths 0
set stopped 1

proc tick {} {
    global seconds hundredths stopped
    if $stopped return
    after 50 tick
    set hundredths [expr $hundredths+5]
    if {$hundredths >= 100} {
	set hundredths 0
	set seconds [expr $seconds+1]
    }
    .counter config -text [format "%d.%02d" $seconds $hundredths]
}

bind . <Control-c> {destroy .}
bind . <Control-q> {destroy .}
focus .