Version 3 of A little stopwatch

Updated 2002-04-15 22:24:00

Richard Suchenwirth - 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. }

 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-$m%10)%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!!

I like the stopwatch written by Don Libes.

[L1 ] ---

Arts and crafts of Tcl-Tk programming