I don't really have a good grasp as to how to efficiently force the program to check every x seconds?
......
It turns out that $mw->after(1000, \&sub); works.. then just add that into the sub to repeat..
Hi, Tk::after is usually used for a one-shot delay. What you want is Tk::repeat. Try
my $repeater = $mw->repeat(1000, \&sub);
....
$repeater->cancel; # when you want to stop it
and you will not need to repeatedly call Tk::after in your sub.
Here is a basic example:
#!/usr/bin/perl
use Tk;
use warnings;
use strict;
my $mw = MainWindow->new(title => "Timer");
my $elapsed_sec = 0;
my $elapsed_sec_label = $mw->Label(-textvariable => \$elapsed_sec)->pa
+ck();
my $repeater; # declare first so it can be accessed in the callback
$mw->Button(-text => "reset",
-command => sub {
$elapsed_sec = 0;
&repeater();
})->pack();
$mw->Button(-text => "exit",
-command => sub { exit })->pack();
# start first run
&repeater();
MainLoop;
sub repeater{
#this is repeated every second, and you can put your is_playing here
$repeater = $mw->repeat(1000 => sub {
$elapsed_sec ++;
if ($elapsed_sec > 4){ $repeater->cancel }
} );
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.