You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.1 KiB
38 lines
1.1 KiB
# timer.tcl - Tk countdown timer |
|
# Usage: timer.tcl <seconds> |
|
# Creates a Tk window displaying the remaining seconds, counts down to zero, |
|
# then exits with code 0. |
|
|
|
if {[catch {package require tk}]} { |
|
package require Tk |
|
} |
|
|
|
if {$::argc < 1} { |
|
puts stderr "Usage: [file tail [info script]] <seconds>" |
|
exit 1 |
|
} |
|
|
|
set seconds [lindex $::argv 0] |
|
if {![string is integer -strict $seconds] || $seconds < 0} { |
|
puts stderr "timer: seconds must be a non-negative integer (got '$seconds')" |
|
exit 1 |
|
} |
|
|
|
# Track the absolute end time so drift in `after` scheduling does not skew the count. |
|
set ::timer_end [expr {[clock milliseconds] + $seconds * 1000}] |
|
|
|
proc timer_tick {} { |
|
set remaining [expr {$::timer_end - [clock milliseconds]}] |
|
if {$remaining <= 0} { |
|
.clock configure -text 0 |
|
exit 0 |
|
} |
|
.clock configure -text [expr {int(($remaining + 999) / 1000)}] |
|
after 100 timer_tick |
|
} |
|
|
|
label .clock -text $seconds -font {TkTextFont 48} -padx 40 -pady 30 |
|
pack .clock -expand 1 -anchor center |
|
|
|
wm title . "Timer: $seconds s" |
|
after 100 timer_tick
|
|
|