我正在尝试用Perl + Tkx创建一个接口,它可以在单击按钮的同时运行外部命令。
关于如何继续使用Tk模块,有很多文档,但是使用Tkx的文档很少。
我仍然找到了一些类似于这一个的东西,但我无法让它适合我的例子。特别是,这些帖子包括使用Tkx::open、Tkx::配置和Tkx::fileevent.但我还没有想出如何把它们结合在一起。
下面是我正在尝试的代码;当单击按钮并按下一个键来终止子进程时,Perl会产生错误Free to wrong pool 16389d0 not 328e448 at C:/Perl/lib/Tcl.pm line 433.。
注意:我使用的是ActivePerl 5.12.2。
use Tkx;
use strict;
my $mw = Tkx::widget->new(".");
my $button=$mw->new_ttk__button(-text => "Run", -command => [\&run_cmd, 0]);
$button->g_grid(-column => 0, -row => 0);
my $text = $mw->new_tk__text(-width => 32, -height => 16);
$text->configure(-state => "disabled");
$text->g_grid(-column => 0, -row => 1);
Tkx::MainLoop();
sub run_cmd {
if (fork()==0) {
system "pause";
exit 0;
}
}谢谢
发布于 2014-12-31 10:47:30
在这个问题上花了将近两天的时间之后,我终于找到了答案,这要归功于一个带有Tcl代码的post 这里,我已经适应了Tkx。
解决方案是使用Tkx::open (结合它的表亲“读”和“关闭”)。
下面的代码可以在不阻塞GUI的情况下正确执行命令,但在大多数情况下,我没有检索STDOUT和STDERR (它用于运行用java开发的应用程序,但不用于systeminfo或diff -v)。
如果有人对此有深入了解,请不要犹豫。
谢谢
use Tkx;
use strict;
use Data::Dumper;
my ($stdout,$stderr);
my $mw = Tkx::widget->new(".");
my $button=$mw->new_ttk__button(-text => "Run", -command => [\&run_command, "systeminfo"]);
$button->g_grid(-column => 0, -row => 0);
my $text = $mw->new_tk__text(-width => 32, -height => 16);
$text->insert("end", "Test\n");
$text->g_grid(-column => 0, -row => 1);
Tkx::MainLoop();
print "STDOUT: $stdout\n\n","-"x24,"\nSTDERR: $stderr\n";
sub run_command {
my $cmd = shift;
my $fh = Tkx::open("| $cmd", 'r') or die "$!";
Tkx::fconfigure($fh, -blocking => 0);
$stdout.=Tkx::read($fh);
eval { Tkx::close($fh); };
$stderr.=$@ if ($@);
}https://stackoverflow.com/questions/27705752
复制相似问题