I need graphical output from a Perl program. The window has a label status field and should show what programming code decides.
How do I change the text of a label field after the window has been created without any buttons?
I have the following:
use Tk;
$mw = Tk::MainWindow->new(-title => 'Status Window',-bg=>"white");
$mw->geometry ("400x200+0+0");
$lala = $mw->Label(-text => "Current Status")->grid(-row=>0,-column=>0);
$mw->bind( 'all' => '<Key-Escape>' => sub {exit;} );
MainLoop;
How do I incorporate the following subroutine so that it is run automatically
after the window is created? Label-Widget does not have
a -command field. It should start immediately and
not wait for a event to happen
sub calculate() {
for ( $i = 0; $i < 10; $i++ ) {
sleep 2s;
$lala->configure(-text=>"Current Status : $i");
}
}
The following seems to work. I used
after()to run code after 100 ms then usedupdate()to redraw window:Edit:
The above code blocks during the
sleep 1call, so any input for theTkevent loop will will be delayed. In particular, pressing Esc to quit the application will not work immediately. It will be blocked untilsleepreturns. To solve this, one can useTk'srepeat()instead ofsleepandTk'safter(), and cancel the repeat if necessary: