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

Hi all,

I'm trying to get a text widget in Tk to be what the STDOUT is for a console script. When I send it an insert I want it to be printed in the text widget realtime. The following code demonstrates my problem (I'm aware that using sleep in a Tk script might not be a very good idea, i'm using it for demonstration purpose only :)

If you run this it looks like it's hanging, eventually it will display the 5 lines. what I want is that it displays the first line, wait for 2 seconds and then display the second etc.

To give an idea what the real script currently does. It resolves all systems known by the domain controller, pings them and displays the active. But currently it waits until the run is completed before displaying the result. I want it to display the result for one system when it is done checking that one.

use Tk; use strict; my $mw = MainWindow->new; $mw->title("Test"); my $frame = $mw->Frame(-bd => 2)->pack; $frame->Button( -text => 'Exit', -command => sub { exit })->pack(-side => 'left'); $frame->Button( -text => 'Excute', -command => \&execute)->pack; my $text = $mw->Scrolled("Text", -scrollbars => 'oe')->pack(-side => 'bottom'); MainLoop; sub execute { foreach my $b (1 .. 5 ) { sleep 2; displayText($text,"$b: Display me\n"); } } sub displayText { my ($text,$output) = @_; $text->insert('end',$output); $text->see('end'); }
Cheers,

Critter: A domestic animal or a non-predatory wild animal.

Replies are listed 'Best First'.
Re: unbuffered Tk text widget?
by zentara (Cardinal) on Oct 10, 2005 at 09:18 UTC
    For your example script, all you need to do is update the $mw after each write.
    sub displayText { my ($text,$output) = @_; $text->insert('end',$output); $text->see('end'); $mw->update; }

    I'm not really a human, but I play one on earth. flash japh
      I knew it was something simple! Many thanks Zentara!

      Cheers,

      Critter: A domestic animal or a non-predatory wild animal.