Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Stopwatch GUI sleep

by welle (Beadle)
on Apr 07, 2011 at 07:29 UTC ( #897958=perlquestion: print w/ replies, xml ) Need Help??
welle has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks

What I'd like: Bind time (clock) events to my program. Let's take this very simple (and poor) program. It's a basic stopwatch (only seconds). Clicking on Start will start counting, clicking on Stop...of course it stops. Counting is the 1st (and only) process. The GUI can be used thanks windows->update. Yet, this makes the app not responsive. If I had sleep 10 (instead of 1), the GUI would respond only every 10 seconds (always updating needs to many resources?). I'd like it to be any time ready to respond.

Now, what is the best approach to solve this? I've no experience with fork. Is fork the right way? Any suggestion would be very appreciated

#!/usr/bin/perl use Tk; use strict; my $count=0; my $window = MainWindow->new; $window->title("My Example"); $window->Label(-textvariable => \$count )->pack; $window->Button(-text => "Start", -command => \&start )->pack; $window->Button(-text => "Stop", -command => \&stop )->pack; MainLoop; ######################################################### my $stop; sub start { while ($count <= 10) { $window->update; $count ++; sleep 1; if ($stop =~ 1) { $stop=0; $count=0; return; } } } sub stop{ $stop=1; }

Comment on Stopwatch GUI sleep
Download Code
Re: Stopwatch GUI sleep
by Corion (Pope) on Apr 07, 2011 at 07:32 UTC

    See Tk::After resp. the Tk::after documentation.

    Most event loop systems and GUI systems don't like sleep because it blocks their event loop processing. In turn, most of these systems implement their own version of sleep as timers that you can then use while still keeping the rest of the system responsive.

Re: Stopwatch GUI sleep
by wind (Priest) on Apr 07, 2011 at 21:28 UTC

    Here's one implementation of Tk::after using a status var to indicate if the counter should be incremented

    #!/usr/bin/perl use Tk; use strict; use warnings; my $count = 0; my $count_on = 0; my $window = MainWindow->new; $window->title("My Example"); $window->Label(-textvariable => \$count )->pack; $window->Button( -text => "Start", -command => sub {$count_on = 1}, )->pack; $window->Button( -text => "Stop", -command => sub {$count_on = 0}, )->pack; my $timer = Tk::After->new($window, 1000, 'repeat', sub { $count++ if +$count_on }); MainLoop;

      Hello Wind, it just works perfectly!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-11-28 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (197 votes), past polls