0

I have a GUI that runs a script from a command button, but how can I get it to display output in the text widget?

If I wanted to display the output via a logfile insert, could I have the command on the same button/sub as the run button?

use warnings;
use strict;
use Tk;
use Tk::Text ;
use POSIX 'strftime';
my $DATE = strftime("Report.pl for %dth %b %Y" , localtime());
my $mw = MainWindow->new;
my $filename = "c:\\Temp\\perl.txt";

$mw->geometry("720x500");
$mw->title("   backupadmin  ");

my $main_frame = $mw->Frame()->pack(-side => 'top', -fill => 'x');

my $left_frame = $main_frame->Frame(-background => "snow2")->pack(-side => 'left', -fill => 'y');
my $right_frame = $main_frame->Scrolled("Text", -scrollbars => 'se',-background => "black",-foreground => "yellow",-height => '44')->pack(-expand => 1, -fill => 'both');


my $failures_button = $left_frame->Button(-text => "  $DATE ",
                                -command => [\&runscript])->pack;
my $Close_button = $left_frame->Button(-text => '                       Close                       ',
-command => [$mw => 'destroy'])->pack;  
my $Help_button = $left_frame->Button(-text => "                  Help Guide                   ",
                                -command => [\&load_file])->pack(-side => "bottom");


my $Close_help = $left_frame->Button(-text => '                  Close Help                      ',
-command => [$right_frame => \&clear_file])->pack(-side => "bottom");                                   


MainLoop;
sub runscript {
  system("report.pl");

}

sub load_file {
  open (FH, "$filename");
  while (<FH>) { $right_frame->insert("end", $_); }
  close (FH);
}

sub clear_file {
    $right_frame->('quit');
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
user3360439
  • 359
  • 1
  • 4
  • 13
  • In English, `I` is always capitalized. So is the first word of every sentence. You don't need the `` tag. You should probably run your prose through a spelling/grammar checker before posting it here. – Robert Harvey Jun 28 '14 at 17:04
  • The question is unclear. This topic might help? http://stackoverflow.com/questions/2461472/how-can-i-run-an-external-command-and-capture-its-output-in-perl – buff Jun 28 '14 at 17:11

1 Answers1

0

If your report.pl script outputs to STDOUT, then you could try something like this in your runscript callback:

sub runscript {
  right_frame->delete('1.0','end');
  my $text = `report.pl`;
  $right_frame->insert('end', $text);
}

Alternatively, if report.pl outputs to c:\temp\perl.txt then you could try the following:

sub runscript {
  right_frame->delete('1.0','end');
  system("report.pl");
  load_file();
}
GeekyDeaks
  • 650
  • 6
  • 12