in reply to Refresh text widget

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]); } }

Replies are listed 'Best First'.
Re: Re: Refresh text widget
by Anonymous Monk on Mar 20, 2002 at 11:18 UTC
    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.