First of all, in Tk (or any gui program) using "sleep" will block the gui, so it's a "no-no". But to show you how to make it work ,in terms of your question, you need to set $|=1 and update your text widget after each print. Like:
#!/usr/bin/perl use warnings; use strict; use Tk; $|=1; my $mw = tkinit; # a frame in my Tk main window my $frame = $mw->Frame; # a text widget with attached scrollbars in my frame my $OutputText = $frame->Scrolled('Text', -height => '10', -width => '50', -scrollbars => 'osoe' ); # pack frame and text widget $frame->pack(qw/-side left -fill y/); $OutputText->pack(qw/-side bottom -fill both -expand 1/); # here I try to tie STDOUT to the text widget my $widget = $OutputText->Subwidget("text"); tie *STDOUT, ref $widget, $widget; $mw->Button(-text=>'start', -command => [\&start])->pack;; MainLoop; sub start{ for ( 1..10 ) { sleep 1; print "foo\n"; $OutputText->update; } } __END__
But here is probably a better way, without using sleep. The reason is that a repeat loop, lets the Tk event loop proceed, and respond to the gui, while sleep will block everything until it is done.
#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); my $tx = $mw->Text()->pack(); tie *STDOUT, 'Tk::Text', $tx; $mw->repeat(1000, \&tick); MainLoop; my $count; sub tick { ++$count; print "$count\n"; }

However, you don't say what you are ultimately trying to do, and tie'ing STDOUT in Tk is not done very often, because there are usually better ways to accomplish it, like printing directly to the text box. What kind of program are you trying to make?


I'm not really a human, but I play one on earth. flash japh

In reply to Re: Tk::Text and buffering, I think by zentara
in thread Tk::Text and buffering, I think by rvosa

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.