in reply to Tk and While Loop
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.
|
|---|