Updated 2013-03-05 02:02:23 by pooryorick

Summary  edit

Richard Suchenwirth 2002-04-15: This stopwatch, a small Tk demo, has a Start button, a MM:SS.CC time display (CC being "centiseconds", or 1/100 of a second), and a Stop button. Precision measured in centiseconds excluded using clock format, as that is only able to render full seconds, so the time is obtained from clock clicks -milliseconds and formatted explicitly from three exprs . Clicking Start of course starts the stop-watch (and disables that button to prevent multiple event chains). Clicking Stop stops the display, to read intermediate times, but internally the clock (expressed in the global time0 variable) keeps ticking. After a second click on Stop, it is reset to 00:00.00.

Some internationalization is also demonstrated. Edit the msgcat::mclocale line to see other languages.

A starkit version of this code is available on sdarchive.

Description  edit

package require Tk
option add *Button.padY 0        ;# to make it look better on Windows
option add *Button.borderWidth 1
#---------------------------------------------------- testing i18n
package require msgcat
namespace import msgcat::mc msgcat::mcset
mcset de Start Los
mcset de Stop  Halt
mcset fr Start Allez
mcset fr Stop  Arrêtez
mcset zh Start \u8DD1
mcset zh Stop  \u505C
msgcat::mclocale en ;# edit this line for display language
#--------------------------------------------------------------- UI
button .start -text [mc Start] -command Start
label  .time -textvar time -width 9 -bg black -fg green 
set time 00:00.00
button .stop -text [mc Stop] -command Stop
eval pack [winfo children .] -side left -fill y
#------------------------------------------------------- procedures
proc every {ms body} {eval $body; after $ms [info level 0]}

proc Start {} {
    if {$::time=="00:00.00"} {
        set ::time0 [clock clicks -milliseconds]
    }
    every 10 {
        set m [expr {[clock clicks -milliseconds] - $::time0}]
        set ::time [format %2.2d:%2.2d.%2.2d \
            [expr {$m/60000}] [expr {($m/1000)%60}] [expr {$m%1000/10}]]
    }
    .start config -state disabled
}
proc Stop {} {
    if {[llength [after info]]} {
        after cancel [after info]
    } else {set ::time 00:00.00}
    .start config -state normal
}

Wow - that is small!!

For everyday use, I like stopwatch written by Don Libes.

Replacing expression for computing seconds
[expr {($m/1000-$m%10)%60}]

to
[expr {($m/1000)%60}]

makes the seconds display work correctly.

RS: Thanks, fixed (but it looked correct before, in my tests...)

MPJ: Also see iLogWatch for a version of this that adds a logging window and the ability to save the start, stop and split times.