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

发布于 2024-11-29 02:59:21 字数 729 浏览 4 评论 0原文

#!/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");
}

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

在此处输入图像描述

然后当我单击“确定”按钮时,它会调用子例程 flush,然后输出显示在终端上,如下所示:

“在此输入图像描述”

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

#!/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");
}

I made this code and what it does is that when a user click on the Button a message box appears

enter image description here

Then when I click on OK button it calls the subroutine flush and then the output is shown on terminal like this:

enter image description here

I want it to be appear on the same message box. How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混浊又暗下来 2024-12-06 02:59:21
  • 不使用系统
  • 捕获 STDOUT/STDERR(qx、IPC::System::Simple、IPC::Run...)
  • 更新标签(就像更新 $textvariable 一样简单......例如,请参阅 Tk 演示程序小部件)

  • don't use system
  • capture STDOUT/STDERR ( qx, IPC::System::Simple, IPC::Run...)
  • update label (as simple as updating $textvariable ... see Tk demo program widget for example )

  • 初相遇 2024-12-06 02:59:21

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

    I have got the answer of this question at perlmonks.

    The link of the post at perlmonks is-> 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');
    }
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文