use warnings; use strict; use Tk; use Time::HiRes qw(time); my ($counter,$timerBtn,$resetBtn,$startMoment,$timer); my $counterText = '0.00'; my $elapsedMS = 0; my $stopped = 1; my $mw = tkinit(); $counter = $mw->Label( -textvariable => \$counterText, -relief => 'raised', -width => 10, -padx => '2m', -pady => '1m' ); $timerBtn = $mw->Button( -text => 'Start', -command => sub { if ($stopped){ $stopped = 0; $startMoment = time(); $timerBtn->configure(-text => 'Stop'); tick(); } else { $timerBtn->configure(-text => 'Start'); $timer->cancel(); } } ); $resetBtn = $mw->Button( -text => 'Reset', -command => sub{ $startMoment = time(); $counterText = '0.00'; } ); $counter->pack(-side => 'bottom', -fill => 'both'); $timerBtn->pack(-side => 'left', -fill => 'both', -expand => 'yes'); $resetBtn->pack(-side => 'right', -fill => 'both', -expand => 'yes'); sub tick{ unless ($stopped){ $timer = $mw->repeat(50, sub{ counter(); }); } } sub counter{ $elapsedMS = (time() - $startMoment); $counterText = sprintf("%.2f",$elapsedMS); } $mw->bind('<Control-c>', sub{ $mw->destroy() }); $mw->bind('<Control-q>', sub{ $mw->destroy() }); $mw->MainLoop();
In reply to Re: A timer in Tcl/Tk in Perl::Tk
by bcarroll
in thread A timer in Tcl/Tk in Perl::Tk
by pashanoid
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |