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.

In reply to unbuffered Tk text widget? by critter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.