exilepanda has asked for the wisdom of the Perl Monks concerning the following question:

In my experience on using Perl/Tk. There are events(mouse, keyboard etc.) to pull the first trigger to do the first thing, and then reflect on the GUI.

But then I have no idea on how to create a trigger at the moment that the GUI starts. For example, to make a clock with arms, the script starts by itself without anyone touch it, then where should I post the block

while(1){ &clockMoveArms($mw) } # for example

in my Perl/Tk code ?

What I am expecting is a while(1){} loop can start with the GUI. Is this feasible or did I missed something ?

Replies are listed 'Best First'.
Re: PerlTk, how to handle/setup an "autorun" block with the GUI?
by zentara (Cardinal) on Feb 22, 2013 at 11:24 UTC
    Here is a simple usage of a timer. Tk::after and Tk::repeat both do the same thing, but Tk::after runs just once, where Tk::repeat continues until you stop it.
    #!/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
      Thank you very much! Your code demonstrated an alternative style from what the perldoc mentioned! Works perfectly and much easier for me to deploy!
Re: PerlTk, how to handle/setup an "autorun" block with the GUI?
by BrowserUk (Patriarch) on Feb 22, 2013 at 08:59 UTC

    See Tk::after


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks Pope! That's exactly what I needed!