Hi,

I have an (long running) application, for which I now want to build a simple GUI, enabling the script to be used even by less experienced users.

Below example demonstrates, what is not happening. I want to capture the output of the main function of the script (printer in the example below) into two separate ROText widgets.

I also want to keep the application "alive" while printer is working - the script below doesn't react on any Event.

Adding $mw->update() is not having the effect I want.

Any hint is appreciated. The script should work on Linux/Unix as well as with (strawberry) Perl on WinXP.

#!/usr/bin/perl use strict; use warnings; use Tk; require Tk::ROText; $|++; {# Main my $mw = MainWindow->new(); $mw->Label(-text => 'STDOUT')->pack(-side => 'top', -anchor => 'w' +); my $ro_out_text = $mw->Scrolled('ROText', -scrollbars => 'osoe', -background => 'white', -width => 50, -height => 15, )->pack(-side => 'top'); tie (*STDOUT, 'Tk::ROText', $ro_out_text); $mw->Label(-text => 'STDERR')->pack(-side => 'top', -anchor => 'w' +); my $ro_err_text = $mw->Scrolled('ROText', -scrollbars => 'osoe', -background => 'white', -width => 50, -height => 15, )->pack(-side => 'top'); tie (*STDERR, 'Tk::ROText', $ro_err_text); $mw->update(); $mw->after(10, [\&printer, $mw]); $mw->repeat(50, sub { $mw->update();}); MainLoop; } sub printer { my $mw = shift @_; my $errcount = 0; my $count = 0; while (1) { if (rand() % 2){ $count++; print "STDOUT: Count = $count\n"; } else { $errcount++; print {*STDERR} "STDERR: Count = $errcount\n"; } sleep (rand() % 500 + 500); } }

Thanks a lot in advance!

EDIT

Here is a working example:

#!/usr/bin/perl use strict; use warnings; use Carp; use IO::Pipe; {# Main if ((scalar @ARGV == 1) and ($ARGV[0] eq '--no-gui')) { printer(); } elsif ((scalar @ARGV >= 1) and ($ARGV[0] ne '--no-gui')) { print "USAGE: $0 [--no-gui]\n"; exit 1; } my $err_pipe = IO::Pipe->new() or croak "Could not create pipe: $! +"; my $out_pipe = IO::Pipe->new() or croak "Could not create pipe: $! +"; if (my $pid = fork ()) { # Child tk_init($err_pipe, $out_pipe); } else { # Father croak "Could not fork: $!" unless defined $pid; $err_pipe->writer(); $out_pipe->writer(); open STDERR, ">&" . $err_pipe->fileno() or croak "Could not re +direct: $!"; open STDOUT, ">&" . $out_pipe->fileno() or croak "Could not re +direct: $!"; printer(); } } sub tk_init { use Tk; use Tk::ROText; my $err_pipe = shift @_; my $out_pipe = shift @_; $err_pipe->reader(); $err_pipe->blocking(0); $out_pipe->reader(); $out_pipe->blocking(0); my $mw = MainWindow->new(); $mw->Label(-text => 'STDOUT')->pack(-side => 'top', -anchor => 'w' +); my $ro_out_text = $mw->Scrolled('ROText', -scrollbars => 'osoe', -background => 'white', -width => 50, -height => 15, )->pack(-side => 'top'); $mw->Label(-text => 'STDERR')->pack(-side => 'top', -anchor => 'w' +); my $ro_err_text = $mw->Scrolled('ROText', -scrollbars => 'osoe', -background => 'white', -width => 50, -height => 15, )->pack(-side => 'top'); $mw->repeat(250, [\&update_text_widget, $ro_out_text, $out_pipe]); $mw->repeat(250, [\&update_text_widget, $ro_err_text, $err_pipe]); $mw->update(); MainLoop(); } sub update_text_widget { my $widget = shift @_; my $handle = shift @_; # slurp data my @in = <$handle>; my $str = join('', @in); $widget->insert('end', $str); return; } sub printer { use Time::HiRes qw(usleep); my $errcount = 0; my $count = 0; while (1) { if (rand(2) % 2 == 1) { $count++; print "STDOUT: Count = $count\n"; } else { $errcount++; print {*STDERR} "STDERR: Count = $errcount\n"; } usleep (rand(500000) + 500000); } }

In reply to (Solved) Perl Tk: STDOUT and STDERR to ROText by Karkadan

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.