在帧而不是终端上获取输出(Perl-Tk)

时间:2011-08-12 19:18:39

标签: linux perl user-interface perltk

#!/usr/local/bin/perl
use Tk;
# Main Window
$mw = new MainWindow;
$label = $mw -> Label(-text=>"Hello folks") -> pack();
$button = $mw -> Button(-text => "Click here to Flush rules",
                -command =>\&flush) -> pack();
MainLoop;

sub flush {
$mw->messageBox(-message=>"Initiating flushing.. click on OK button");
system ("iptables -L");
system ("iptables -F");
system ("iptables -L");
}

我制作了这段代码,它的作用是当用户点击按钮时出现一个消息框

enter image description here

然后当我点击OK按钮时,它调用子程序flush,然后输出显示在终端上,如下所示:

enter image description here

我希望它出现在同一个消息框中。我该怎么办?

2 个答案:

答案 0 :(得分:1)

  • 不要使用系统
  • 捕获STDOUT / STDERR(qx,IPC :: System :: Simple,IPC :: Run ...)
  • 更新标签(就像更新$ textvariable一样简单...例如,请参阅Tk演示程序小部件)

  • 答案 1 :(得分:0)

    我在perlmonks得到了这个问题的答案。

    perlmonks的帖子链接是> http://www.perlmonks.org/index.pl?node_id=920414

    #!/usr/bin/perl
    use warnings;
    use strict;
    use Tk;
    
    # Main Window
    my $mw = new MainWindow;
    $mw->geometry('+100+100');
    
    my $label = $mw -> Label(-text=>"Hello folks") -> pack();
    my $button = $mw -> Button(-text => "Click here to Flush rules",
                    -command =>\&flush) -> pack();
    MainLoop;
    
    
    sub flush {
    $mw->messageBox(-message=>"Initiating flushing.. click on OK button");
    # the script hangs here, until the messagebox OK button is pressed.
    
    my $text = $mw->Scrolled('Text')->pack();
    
    #my $out1 =  `iptables -L`;
    my $out1 =  `ls -la`;
    $text->insert('end',"$out1\n");
    $text->see('end');
    
    #my $out2 =  `iptables -F`;
    my $out2 =  `dir`;
    $text->insert('end',"$out2\n");
    $text->see('end');
    
    #my $out3 =  `iptables -L`;
    my $out3 =  `ps auxww`;
    $text->insert('end',"$out3\n");
    $text->see('end');
    }