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

Hello, I need to start subroutine before MainLoop() (its a while loop) This is the sub: --------
sub CheckTime { $exeTime=18; $Hour=shift(@_); $loopStop=0; while ($loopStop==0) { sleep(1); # 1 = 1s ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time +); $ZWIDGETS{'Text1'} -> insert('end',"Waiting......\n"); if ($hour==$exeTime) { $loopStop=1; $ZWIDGETS{'Text1'} -> insert('end' +,"Starting.....\n");} } return $hour; }
---------------------------------------- And when I put it in this way, first program do the while loop and than start window: -----------------------------------------
&CheckTime($HourGet); MainLoop();
----------------------------------------

I need the subroutine works all the time and update Tk Text box...

Is there sth in perl like onForm_load???

I have try use the threads but Tk and threads works really bad.

Thx for help..

20060808 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Tk and While Loop
by jdtoronto (Prior) on Aug 08, 2006 at 16:38 UTC
    Your code and explanation is a little confusing, but here goes.

    If you go off into a while() like that you will slow down the main loop drastically. How often do you need to check the time? Look at Tk::after in the Tk docs which will tell you how to include a delay loop or a repetitive loop in the Tk mainloop function.

    To update the GUI, just use $mw->update(); where $mw is the MainWindow and the GUI elements will update. Put the update in the loop.

    jdtoronto

Re: Tk and While Loop
by liverpole (Monsignor) on Aug 08, 2006 at 18:11 UTC
    Hi lukaszt,

    When you program in Tk, you have to change your mindset to think in terms of event-driven programming.

    Here's an example of how you could use the Tk "repeat" function, to execute a command every second:

    # Create a new main window object called $mw my $mw = new MainWindow(); # ... other GUI setup here # Call repeat to execute your subroutine every second (1000 millisecon +ds) my $loopStop = 0; # A pointer to this will be passed to the subrout +ine my $hour; # However this is calculated ... $mw->repeat(1000 => [ \&CheckTime, \$loopStop, $hour ]); MainLoop(); # The MainLoop goes last # And here's the subroutine + sub CheckTime { my ($ploopStop, $hour) = @_; $exeTime=18; if ($$ploopStop == 0) { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); + $ZWIDGETS{'Text1'} -> insert('end',"Waiting......\n"); if ($hour == $exeTime) { $ploopStop = 1; $ZWIDGETS{'Text1'}->insert('end', "Starting.....\n"); } } return $hour; }

    Note that you don't need to sleep in the subroutine; that would only slow down the entire GUI, as jdtoronto pointed out.

    When the value of $hour (however you are calculating it) is equal to $exeTime, the variable pointed to by $ploopStop is set to 1, so on subsequent calls, the logicthe documentation for Tk::repeat to see how to cancel an event loop.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Tk and While Loop
by GrandFather (Saint) on Aug 08, 2006 at 21:16 UTC

    liverpole has answered your question, but there are a few tips that may help you in general.

    • Use strictures. Add use strict; use warnings; at the top of every script you write. They will save you time.
    • Convince your editor to use spaces for indentation rather than tabs. Your code looks a mess because the tabs have evaporated.
    • Avoid putting multiple statements on a line. Unless you are writing on a PDA, screen lines are not generally an issue and putting stuff where it can be seen makes a big difference to understanding - especially when other people may have to work with you code.

    DWIM is Perl's answer to Gödel