in reply to Trying to update a Tk:ROText widget as input is received from a backticked program

You really should use fileevent to read any sort of filehandle with Tk. Here is an example to demonstrate. I used IPC3 instead of a piped open because it is just eaiser to deal with the separate STDOUT and STDERR from the pipe. If you want to use the piped open form, you may want to look into the 2>&1 bash shell trick to combine your stdout and stderr. IPC3 makes it easier to keep them separate. Also in this example, there is some additional error checking you need to do, like preventing the program from crashing if you kill the toplevel. I left that out for simplicity.

Here is the sender script for testing (also leftout ROText, but that is easy):

#!/usr/bin/perl use warnings; use strict; $| = 1; my $count = 0; while(1){ $count++; print "$count\n"; warn "\tuh oh warning $count\n"; sleep 1; }

And here is the IPC3-Tk example.

#!/usr/bin/perl use warnings; use strict; use IPC::Open3; use Tk; my $mw = new MainWindow; $mw->geometry("600x400"); $mw->Button(-text => "See STDERR", -command => \&do_Toplevel)->pack(); my $tout = $mw->Scrolled( 'Text', -foreground => 'white', -background => 'black', -width => 80, -height => 20, )->pack; my $top = $mw->Toplevel(); $top->withdraw; my $terr = $top->Scrolled( 'Text', -foreground => 'hotpink', -background => 'black', -width => 80, -height => 20, )->pack; my $pid = open3( 0, \*OUT, \*ERR, "$0-sender" ); #the 0 is for ignoring \*IN (STDIN) $mw->fileevent( \*OUT, 'readable', \&write_out ); $mw->fileevent( \*ERR, 'readable', \&write_err ); MainLoop; ##################################################### sub do_Toplevel { if (! Exists($top)) { $top = $mw->Toplevel( ); $top->title("T-ERR"); $top->Button(-text => "Close", -command => sub { $top->withdraw })->pack; } else { $top->deiconify( ); $top->raise( ); } } ############################################################# sub write_out { my $str = <OUT>; $tout->insert( "1.0", $str ); $tout->see("1.0"); } #################################################### sub write_err { my $str = <ERR>; $terr->insert( "1.0", $str ); $terr->see("1.0"); } __END__

I'm not really a human, but I play one on earth. flash japh
  • Comment on Re: Trying to update a Tk:ROText widget as input is received from a backticked program
  • Select or Download Code