That's helpful, thank you. Now I'd like to redirect STDOUT and STDERR from an external program to my text widget in real time. The example below writes to the text widget, but not in real time AND the STDERR is written first rather than last. (the example below does not exhibit this last problem.) I've parsed many solutions and am thoroughly confused at this point.
#!/usr/bin/perl -w
use warnings;
use strict;
use Tk;
use Tk::NoteBook;
require Tk::Pane;
use Tie::IxHash;
use Tk::LabEntry;
my $mw = MainWindow->new;
$mw->geometry( "600x500");
$mw->title("Survival Kit");
##################NOTES########
##expansion from 2nd tab has bug?
###default hash
tie my %tools, 'Tie::IxHash',
'program1' => ordered_hash_ref ( 'PARM1' => 'Y', 'PARM2' => 'files', '
+PARM3' => 'list', 'PARM4' => 'Y', 'PARM5' => 'N'),
'program2' => ordered_hash_ref ( 'PARM' => '', 'PARM0' => 0, 'PARM1' =
+> 1, 'PARM2' => 2, 'PARM3' => 3, 'PARM4' => 4, 'PARM5' => 5, 'PARM6'
+=> 6, 'PARM7' => 7, 'PARM8' => 8, 'PARM9' =>9, 'PARM10' => 10 );
+
## create notebook
my $book = $mw->NoteBook()->pack(-expand=>1,-fill=>'both');
#####program1#######
my $tab_cnt=1;
for my $tab (keys %tools){
##create tab,scroll pane,frames
my $tab_tab=$book->add("Sheet $tab_cnt", -label => "$tab")->pack
+(-expand=>1,-fill=>'both');
my $tab_spane=$tab_tab->Scrolled('Pane',-background=>'slate grey
+')->pack(-expand=>1, -fill=>'both');
my $tab_frame1=$tab_spane->Frame(-background=>'blue')->pack(-sid
+e=>'left',-expand=>1,-fill=>'both',-padx=>15);
my $tab_frame2=$tab_spane->Frame(-background=>'red')->pack(-side
+=>'bottom',-anchor=>'s',-fill=>'x');
$tab_cnt++;
#create columns
my $tab_column1 = $tab_frame1->Frame()->pack(-side=>'left',-exp
+and=>1,-fill=>'both');
my $tab_column2 = $tab_frame2->Frame()->pack(-side=>'right',-ex
+pand=>1,-fill=>'both');
##now fill frames
my $parm_cnt=1;
#print "Processing tool: $tab\n";
foreach my $parm ( keys %{$tools{$tab}}) {
#print " $parm = $tools{$tab}{$parm}\n";
my $tab_test;
if ($parm_cnt < 7 ){
$tab_test=$tab_column1->LabEntry(-label => "$parm="
+);
$tab_test->Subwidget('entry')->configure(-textvariable =>
+ \$tools{$tab}{$parm} );
$tab_test->configure(-labelPack=>[-side=>'left']);
$tab_test->pack(-anchor=>'e');
} else {
$tab_test=$tab_column2->LabEntry(-label => "$parm="
+);
$tab_test->Subwidget('entry')->configure(-textvariable =>
+ \$tools{$tab}{$parm} );
$tab_test->configure(-labelPack=>[-side=>'left']);
$tab_test->pack(-anchor=>'e');
}
$parm_cnt++;
}
#######go and save button#############
my $run=$tab_tab->Button(-text => "\n $tab \n ",-command=>
+\&go ,-command =>[\&save_parms,$tab],-background=>'slate grey')->pack
+;
}
MainLoop;
####################### subs ###############################
sub save_parms {
my $tab = shift;
#print $tab;
#program > FILE 2>&1
#open STDOUT,">tmpfile" or die "Cannot open temp file";
#open STDOUT, '>/dev/null';
#open STDERR, '>>&STDOUT';
#close STDOUT;
####stderr stdout dialog box###################
my $popup = Tk::MainWindow->new;
$popup->title("${tab} Info");
my $text = $popup->Scrolled('Text',-label => "Output/Errors",-width =
+> 45,-height => 20);
$text->pack;
$| = 1;
tie(*STDERR, 'Tk::Text', $text);
tie(*STDOUT, 'Tk::Text', $text);
$SIG{'__WARN__'} = sub { print STDERR @_ };
my $button = $popup->Button(
-text => 'Close',
-command => [$popup=> 'destroy']);
$button->pack;
################################################
if ( "$tab" eq "program1"){
open (my $parfile,"> program1_parms.txt") or warn "$!\n";
foreach my $parm ( keys %{$tools{$tab}} ) {
print $parfile "$parm=$tools{$tab}{$parm}\n";
}
close $parfile;
print STDOUT `cat program1_parms.txt 2>&1 `;
sleep 5;
print STDERR "HELP!";
}
}
## order hashes##
sub ordered_hash_ref {
tie my %hash, 'Tie::IxHash', @_;
return \%hash;
}
-honyok
|