timer 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 Tcl 8.4
  7. package require Tk
  8. label .counter -text 0.00 -relief raised -width 10 -padx 2m -pady 1m
  9. button .start -text Start -command {
  10. if {$stopped} {
  11. set stopped 0
  12. set startMoment [clock clicks -milliseconds]
  13. tick
  14. .stop configure -state normal
  15. .start configure -state disabled
  16. }
  17. }
  18. button .stop -text Stop -state disabled -command {
  19. set stopped 1
  20. .stop configure -state disabled
  21. .start configure -state normal
  22. }
  23. pack .counter -side bottom -fill both
  24. pack .start -side left -fill both -expand yes
  25. pack .stop -side right -fill both -expand yes
  26. set startMoment {}
  27. set stopped 1
  28. proc tick {} {
  29. global startMoment stopped
  30. if {$stopped} {return}
  31. after 50 tick
  32. set elapsedMS [expr {[clock clicks -milliseconds] - $startMoment}]
  33. .counter config -text [format "%.2f" [expr {double($elapsedMS)/1000}]]
  34. }
  35. bind . <Control-c> {destroy .}
  36. bind . <Control-q> {destroy .}
  37. focus .
  38. # Local Variables:
  39. # mode: tcl
  40. # End: