in reply to Perl/Tk windows and status
That will allow the MainLoop and window to get up and running, before you start your long startup process.$mw->after(1, sub {&startup()}); MainLoop;
Alternatively you can use waitvisibility, which tells the MainLoop to get the window up before proceeding past that point in the code.
So here is an example, using a simple sleep. However, this is where your REAL problem lies, how to get the STDOUT from your startup processes into your textbox. I've shown 2 subs. One is the standard insert with update, but that won't do you any good if some other program is writing to STDOUT. (Unless you use piped opens, IPC::Open3, etc)
But there is a trick for tieing STDOUT to the test widget, which I have shown. Now, you still need to update the text widget, which I have done in the simple loop. But in your real application startup, you will probably need to resort to a timer which updates the text widget on an interval.
#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = tkinit; my $textbox = &do_Toplevel(); #to wait until a $widget is visible, use waitVisibility: $mw->waitVisibility; #your long startup code, telling it which textbox to display in #&startup($textbox); &startup1($textbox); MainLoop; ################################################################# sub do_Toplevel { my $tl = $mw->Toplevel(); $tl->title( "Early Status" ); my $text = $tl->Scrolled('Text')->pack(); $text->insert('end',"........startup messages.........\n"); $tl->Button( -text => "Close", -command => sub { $tl->withdraw } )->pack; return $text; } ############################################################## sub startup{ my $textbox = shift; foreach my $num (1..10){ $textbox->insert('end',"$num\n"); $textbox->update; sleep 1; } } ################################################################## sub startup1{ my $textbox = shift; $|++; tie *STDOUT, 'Tk::Text', $textbox; #may need this to force repeated updates # $mw->repeat(10,sub{ $textbox->update }); foreach my $num (1..10){ print "$num\n"; # printing to STDOUT $textbox->update; sleep 1; } } __END__
|
---|