如何用Tcl/Tk语言实现带文本模式控制的计时器(仿指定Bash脚本)
Got it, let's put together a Tcl console-mode timer that mirrors your Bash script's functionality! This implementation will update in real-time, write the current time to time.txt on every tick, and include simple text-mode controls—plus, just like your shell version, any minor timing inaccuracies are totally imperceptible to humans.
Here's the full script:
#!/usr/bin/env tclsh # Tcl text-mode timer (matches functionality of your Bash shell timer) # Note: Minor timing inaccuracies exist due to event loop overhead, but they're imperceptible to humans set s 0 set m 0 set h 0 set running 1 proc update_timer {} { global s m h running if {!$running} { return } # Increment time values incr s if {$s >= 60} { set s 0 incr m if {$m >= 60} { set m 0 incr h } } # Format time to HH:MM:SS with leading zeros set time_str [format "%02d:%02d:%02d" $h $m $s] # Clear terminal screen (use `exec cls` instead for Windows systems) exec clear >@stdout # Print current elapsed time puts "Elapsed Time: $time_str" # Write updated time to time.txt set f [open "time.txt" w] puts $f $time_str close $f # Schedule next update in 1 second after 1000 update_timer } proc handle_input {} { global running set key [gets stdin] # Handle user commands switch -exact -- $key { q { set running 0 puts "\nTimer stopped. Final time saved to time.txt" exit } p { set running [expr {!$running}] if {$running} { puts "\nTimer resumed..." update_timer } else { puts "\nTimer paused. Current time saved to time.txt" } } default { puts "\nUnknown command - press 'q' to quit, 'p' to pause/resume" # Re-enable input handling after invalid command fileevent stdin readable handle_input } } } # Initialize and start the timer puts "Tcl Text-Mode Timer started!" puts "Press 'q' to quit, 'p' to pause/resume\n" update_timer # Set up input handling for user controls fileevent stdin readable handle_input # Keep the script running in the event loop vwait forever
Core Features & How It Works
- Real-time updates: Uses Tcl's built-in
aftercommand to trigger a time update every 1000ms (1 second), just like your Bash script's loop. - Clean display: The
exec clearcommand refreshes the terminal each tick to keep the time display focused (swap withexec clsif you're on Windows). - Persistent time logging: Every update overwrites
time.txtwith the current formatted time, matching your original script's behavior. - Proper time formatting: The
formatcommand ensures we get consistentHH:MM:SSoutput with leading zeros for single-digit values.
Usage Controls
- Press
qat any time to stop the timer and exit—your final elapsed time will be saved totime.txt. - Press
pto pause or resume the timer; the current time is saved totime.txteven when paused.
A Quick Note on Accuracy
Just like your Bash script, there's a tiny bit of overhead from the event loop and file I/O operations. But as you pointed out, this is completely unnoticeable to humans—this timer is perfect for everyday timing needs where precision down to the millisecond isn't required.
内容的提问来源于stack exchange,提问作者Diego Henrique
相关产品推荐
相关产品推荐

