in reply to Perl/Tk C++ Interaction

I'm substituting a Perl sender script for your C++ program here, but this is the idea. You may need to use sysread, instead of the <> operator, there may be blocking problems in the pipes on win32 platforms, and error checking is needed. P.S. To control your C++ program, print to the STDIN filehandle.... i didn't include that feature here because I'm lazy and didn't want to add listener code into the sender script. But look at IPC3 buffer limit problem for a non-gui full control over a script.

sender_program script ( like your C++ program)

#!/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 the Tk reader script

#!/usr/bin/perl use warnings; use strict; use IPC::Open3; use Tk; use Tk::ProgressBar; my $mw = new MainWindow; $mw->geometry("600x400"); my $tframe = $mw->Frame()->pack(); my $count = 0; my $l1 = $tframe->Label(-text => '%', -bg => 'black', -fg => 'green', )->pack(-side => 'left'); my $l2 = $tframe->Label( -text => $count, # -text option better here, than textvariable which # may cause unicode display problem #-textvariable => \$count, #may cause unicode display problem over a socket -bg => 'black', -fg => 'green', -width => 3, )->pack(-side => 'left'); my $p = $tframe->ProgressBar( -troughcolor => 'black', -fg => 'lightgreen', -blocks => 1, -width => 20, -length => 200, -from => 0, -to => 100, -variable => \$count, )->pack(-side =>'right'); my $tout = $mw->Scrolled( 'Text', -foreground => 'white', -background => 'black', -width => 80, -height => 20, )->pack; $tout->tagConfigure( 'red', -foreground => 'red' ); my $pid = open3( 0, \*OUT, \*ERR, "sender_program" ); #the 0 is for ignoring \*IN (STDIN) $mw->fileevent( \*OUT, 'readable', \&write_out ); $mw->fileevent( \*ERR, 'readable', \&write_err ); MainLoop; ############################################### sub write_out { $count = <OUT>; chomp $count; # eval $count; #prevents raw unicode, you may not need it # print "$count\n"; $l2->configure( '-text' => $count); # works better than textvaria +ble $tout->insert( "end", "$count\n" ); $tout->see("end"); } ############################################ sub write_err { my $str = <ERR>; $tout->insert( "end", "\t$str", 'red' ); $tout->see("end"); } __END__

I'm not really a human, but I play one on earth Remember How Lucky You Are

Replies are listed 'Best First'.
Re^2: Perl/Tk C++ Interaction
by dmarg (Initiate) on Dec 01, 2008 at 18:29 UTC
    That works!! Thanks.
      See the P.S. I just added, I didn't show how to control the external program by writing to it's STDIN, but it is easily added. Just remember, in Tk, use fileevent instead of IO::Select to watch filehandles.

      I'm not really a human, but I play one on earth Remember How Lucky You Are