Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Refresh text widget

by MadMax (Novice)
on Mar 20, 2002 at 04:48 UTC ( [id://152914]=perlquestion: print w/replies, xml ) Need Help??

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

I want to have a widget (text window) that will show the progress of a running script. I can build the widget, but the text that I want installed line-by-line will not show up in the window until the entire process is complete.

Please help me to figure out what I am doing wrong in inserting the text into the text widget. What I get is the total output at once and what I want is a line-by-line insert into the widget window. Here is an example of what I am doing.

----------------------------

#!/usr/local/bin/perl use Tk; my $submain = MainWindow->new(); $status = $submain->Button(-text => 'Click to Clear', -command => sub {$submain->destroy;} ); $status->pack; $text1 = $submain->Text ('-width'=>100, 'height'=>20); $text1->pack(-side => 'left', -fill => 'y'); $text1->insert('end', "start\n"); $text1->pack; for(my $loop=0; $loop <= 3; $loop++ ) { sleep 1; print "$loop\n"; if ( $loop == 1 ) { $line = "$loop case 1\n"; } elsif ( $loop == 2 ) { $line = "$loop case 2\n"; } else { $line = "$loop something else\n"; } $text1->insert('end', "$line"); $text1->pack; } MainLoop();

Replies are listed 'Best First'.
Re: Refresh text widget
by Anonymous Monk on Mar 20, 2002 at 09:39 UTC

    Well, for starters, in Tk land, anything you do before you execute MainLoop is completed (in most cases) before you see anything displayed. I would add a start button to the widget that starts the loop. Then I would get away from sleep and the for..next. I would use a timer (GUIs should *almost* never block) that repeatedly calls the subroutine that handles the counting and printing.

    If you do this, then the loop will not be started until the GUI is up. And your display should be updated since $widget->after() does not put the interface (GUI) to sleep.

    I have tested the following modifications, and they work on my system...

    #!/usr/local/bin/perl use strict; use Tk; my $Mycount = 0; my $submain = MainWindow->new(); my $buttons = $submain->Frame()->pack(-side => 'top'); my $status = $buttons->Button(-text => 'Click to Clear', -command => sub {$submain->destroy;} )->pack(-side=>'left'); my $text1 = $submain->Text ('-width'=>100, 'height'=>20); $text1->pack(-side => 'left', -fill => 'y'); my $doit = $buttons->Button(-text => 'Do It', -command => [ \&theLoop => $submain, \$Mycount], )->pack(-side=>'left'); $text1->insert('end', "start\n"); $text1->pack; MainLoop(); sub theLoop { my $widget = shift; my $Mycount = shift; my $line = ''; $$Mycount++; #for(my $loop=0; $loop <= 3; $loop++ ) ## Looping handled by timer +... #{ #sleep 1; Incompatible with Tk MainLoop - It pauses the *enti +re* application print "$$Mycount\n"; if ( $$Mycount == 1 ) { $line = "$$Mycount case 1\n"; } elsif ( $$Mycount == 2 ) { $line = "$$Mycount case 2\n"; } else { $line = "$$Mycount something else\n"; } $text1->insert('end', "$line"); #$text1->pack; #} ## Here is where we implement the delay.... and the loop ## We simply check to see if our loop max has been reached, then i +f not, ## set a future event to call this subroutine again. ## The 500 is the number of milliseconds to wait before calling. ## This is better programming in Tk, since the sleep command stops + everything ## which is very bad with a GUI. (think M$ when a low level call h +appens) if ($$Mycount < 3) { my $delay = $submain->after(500, [\&theLoop => $widget, $Mycou +nt]); } }
      Thank you very much.     Yudaman!!

      BTW, the sleep was just to slow down the sample script enough to give me time to see that the gui wasn't posting the data as it is generated.  It isn't actually part of the script.

      I believe that your approach will work just fine.  This is actually intended to be a part of a larger multiple destination FTP tool and the information to be displayed is the results of the individual FTP sessions as they are happening (hence the simulated time lag with the sleep command).  I think you have cleared the cobwebs from my head well enough for me to get on with it.  I actually have everything else completed and working properly, I just wanted to add the "feel good" feedback that the FTP sessions are continuing without the user sitting and wondering if the process has failed ot not.

      Thanks again.

Re: Refresh text widget
by ChOas (Curate) on Mar 20, 2002 at 07:53 UTC
    Hi!

    First I would like to advice you to use
    #!/usr/local/bin/perl -w use strict;
    Then we`ll talk further.. the first thing you`ll probably
    see is that your program won`t run because Line is not
    declared, and when you declare it, please do not do it
    within the else statement, because that will make it too
    local to insert in your widget.

    But, like I said, use strict, and -w, fix the errors, and
    see if that helps, we`ll take it from there, okay ?

    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;
      OKAY ... thank you for the advice to use strict (which is in the original code for this extracted example). I have modified this example code with the appropriate lines. It still works in the same undesired manner. Any hints towards what the real issue is?
      #!/usr/local/bin/perl -w use strict; use Tk; my $line = ""; my $submain = MainWindow->new(); my $status = $submain->Button(-text => 'Click to Clear', -command => sub {$submain->destroy;} ); $status->pack; my $text1 = $submain->Text ('-width'=>100, 'height'=>20); $text1->pack(-side => 'left', -fill => 'y'); $text1->insert('end', "start\n"); $text1->pack; for(my $loop=0; $loop <= 3; $loop++ ) { sleep 1; print "$loop\n"; if ( $loop == 1 ) { $line = "$loop case 1\n"; } elsif ( $loop == 2 ) { $line = "$loop case 2\n"; } else { $line = "$loop something else\n"; } $text1->insert('end', "$line"); $text1->pack; } MainLoop();

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://152914]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 18:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found