1#!/bin/sh
2# the next line restarts using wish \
3exec wish "$0" "$@"
4
5# timer --
6# This script generates a counter with start and stop buttons.
7#
8# RCS: @(#) $Id$
9
10package require Tcl 8.4
11package require Tk
12
13label .counter -text 0.00 -relief raised -width 10 -padx 2m -pady 1m
14button .start -text Start -command {
15    if {$stopped} {
16	set stopped 0
17	set startMoment [clock clicks -milliseconds]
18	tick
19	.stop configure -state normal
20	.start configure -state disabled
21    }
22}
23button .stop -text Stop -state disabled -command {
24    set stopped 1
25    .stop configure -state disabled
26    .start configure -state normal
27}
28pack .counter -side bottom -fill both
29pack .start -side left -fill both -expand yes
30pack .stop -side right -fill both -expand yes
31
32set startMoment {}
33
34set stopped 1
35
36proc tick {} {
37    global startMoment stopped
38    if {$stopped} {return}
39    after 50 tick
40    set elapsedMS [expr {[clock clicks -milliseconds] - $startMoment}]
41    .counter config -text [format "%.2f" [expr {double($elapsedMS)/1000}]]
42}
43
44bind . <Control-c> {destroy .}
45bind . <Control-q> {destroy .}
46focus .
47
48# Local Variables:
49# mode: tcl
50# End:
51