Welcome, guest | Sign In | My Account | Store | Cart

This stopwatch works on Tcl/Tk 8.5 and higher. It makes use of the [clock] command and reduced the amount of code by -textvariable. A simple one, indeed. Nevertheless, it is still easy-to-read and easy-to-understand and it does show the power that Tcl has.

Tcl, 26 lines
 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
set seconds {}
set timetext 00:00:00
set id {}
ttk::label .time -textvariable timetext -font {Arial 32}
ttk::button .reset -text Reset -command reset
ttk::button .start -text Start -command {
    reset
    set seconds -1
    setSeconds
}
proc setSeconds {} {
    set ::id [after 1000 setSeconds]
    incr ::seconds
}
proc reset {} {
    if {$::id != {}} {
        after cancel $::id
    }
    set ::id {}
    set ::seconds 0
}
trace add variable seconds write updateTime
proc updateTime {args} {
    set ::timetext [clock format $::seconds -gmt 7 -format %H:%M:%S]
}
pack .time .reset .start
Created by Mike Manilone on Fri, 5 Sep 2014 (CC0)
Tcl recipes (162)
Mike Manilone's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks