Perl Tk:调用外部脚本时,如何防止其他按钮被点击?



OS: Linux( Scientific Linux ) 语言和版本: Perl 5.24

1.当我单击按钮时,调用外部脚本,例如

$mw->Button(-command => sub { 
$value = `/root/desktop/script.pl`; chomp($value); 
} )-> grind();
  1. 外部脚本的GUI将弹出并让我填充值。

  2. 我还没有完成外部脚本,但我"快速单击"主脚本中的一个按钮(如果我单击它,它会弹出一个窗口,但只有在外部脚本关闭后)。

  3. 我关闭了外部脚本。

  4. 关闭外部脚本后,大量窗口一次从主脚本中弹出一个。

关闭我调用的外部脚本后,如何防止其他按钮/小部件大量弹出窗口?

下面是一个脚本,该脚本在启动命令之前禁用所有按钮,然后在命令退出时重新启用它们:

#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use Tk;
use Tk::Table;
use IO::Handle;
use Data::Dumper;
my $mw= tkinit;
my @buttons;
push @buttons, $mw->Button(
-text     => 'Button One',
-command  => &action_one,
)->pack;
push @buttons, $mw->Button(
-text     => 'Button Two',
-command  => &action_two,
)->pack;
push @buttons, $mw->Button(
-text     => 'Button Three',
-command  => &action_three,
)->pack;
MainLoop;
exit;

sub action_one {
run_external_command('xclock');
}

sub action_two {
run_external_command('xcalc');
}

sub action_three {
run_external_command('xlogo');
}
sub run_external_command {
disable_buttons();
open my $fh, '-|', @_;
$mw->fileevent($fh, 'readable', sub { command_read($fh) });
}

sub command_read {
my($fh) = @_;
my $buf = '';
if ( sysread($fh, $buf, 4096) ) {
print "Read: '$buf'";
}
else {
close($fh);
enable_buttons();
}
}

sub disable_buttons {
foreach my $b (@buttons) {
$b->configure(-state => 'disabled');
}
}

sub enable_buttons {
foreach my $b (@buttons) {
$b->configure(-state => 'normal');
}
}

最新更新