Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Streaming into tk text widgets

by Scarborough (Hermit)
on Oct 14, 2004 at 13:39 UTC ( [id://399206]=perlquestion: print w/replies, xml ) Need Help??

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

Thanks to the monks for the answer to Streaming to a socket it worked brilliently when my client was working at the command promt.

while (defined($buf = <$sock>){ print $buf; }

However I have tried to display this in a Tk gui but the text does not display until all the data has been returned. This is what I have tried

use IO::Socket; use Tk; my $mw = new MainWindow(); my $b = $mw->Button(-text=>'GO', -command=>\&conn)->pack(); my $t = $mw->Text()->pack(); #$|++; #tie *OUT, 'Tk::Text', $t; $t->focus(); MainLoop; sub conn{ tie *STDOUT, 'Tk::Text',$t; $|++; $sock = new IO::Socket::INET(PeerAddr => 'myhost', PeerPort => '800', Proto => 'tcp'); die unless $sock; while (defined($buff = <$sock>)){ print $buff; } }

Is it possible to get a live stream to the widget?

Replies are listed 'Best First'.
Re: Streaming into tk text widgets
by si_lence (Deacon) on Oct 14, 2004 at 13:57 UTC
Re: Streaming into tk text widgets
by zentara (Archbishop) on Oct 15, 2004 at 12:59 UTC
    You want to use Tk::fileevent. It is like IO::Select for Tk. This is untested, but should show you the idea. I'm not sure how your tie *STDOUT will work here. What fileevent does, is test for data in a filehandle, and if "readable", will read it and insert it into your Text widget. It is non-blocking, so your GUI will continue to run.
    sub conn{ tie *STDOUT, 'Tk::Text',$t; $|++; $sock = new IO::Socket::INET(PeerAddr => 'myhost', PeerPort => '800', Proto => 'tcp'); die unless $sock; # while (defined($buff = <$sock>)){ # print $buff; # } $mw->fileevent($sock, 'readable', [\&fill_text_widget,$t]); } sub fill_text_widget { my($widget) = @_; $_ = <$sock>; $widget->insert('end', $_); $widget->yview('end'); }

    I'm not really a human, but I play one on earth. flash japh
      Thanks to both of you for this help its given me the final piece to complete my little application.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-18 02:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found