perl tk gui在文本小部件中显示脚本stdout和stderr

时间:2014-06-28 17:03:30

标签: perl

我有一个从命令按钮运行脚本的GUI,但是如何让它在文本小部件中显示输出?

如果我想通过日志文件插入显示输出,我可以在与运行按钮相同的按钮/子上显示命令吗?

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');
}

1 个答案:

答案 0 :(得分:0)

如果你的report.pl脚本输出到STDOUT,那么你可以在你的runcript回调中尝试这样的事情:

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

或者,如果report.pl输出到c:\ temp \ perl.txt,那么您可以尝试以下操作:

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