操作系统: Linux (科学Linux )语言和版本: Perl 5.24
1.当我单击一个按钮时,调用外部脚本,如
$mw->Button(-command => sub {
$value = \`/root/desktop/script.pl\`; chomp($value);
} )-> grind();在关闭我调用的外部脚本之后,如何防止窗口从其他按钮/小部件中大量弹出?
发布于 2017-04-11 22:14:25
下面的脚本在启动命令之前禁用所有按钮,然后在命令退出时重新启用它们:
#!/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');
}
}https://stackoverflow.com/questions/43348277
复制相似问题