Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: TK Timed action

by zentara (Archbishop)
on Sep 25, 2012 at 15:30 UTC ( #995570=note: print w/ replies, xml ) Need Help??


in reply to TK Timed action

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 } } ); }

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh


Comment on Re: TK Timed action
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://995570]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2015-07-08 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls