timer 1.1 KB

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