原文:
18.4. How do I update widgets while waiting for other processes to complete?
The short answer is either $widget -> update;or $widget -> DoOneEvent;
Here is a script that makes use of the first of these methods. Note that instead of actually doing something useful the "long running process" is simply a call to the perl sleep() function for illustrative purposes: #!/usr/bin/perl -w use Tk; my $m = MainWindow->new(); my $l = $m -> Listbox(); $l -> bind('<Double-1>' => sub{sleepy($l)} ); my @nuts = qw(Almond Brazil Chestnut Doughnut Elmnut Filbert); for (@nuts) { $l -> insert('end',$_); } $l -> pack; MainLoop; sub sleepy { my $widget = shift; print "before 1st sleep \n"; sleep(10); print "after 1st sleep before delete \n"; $widget -> delete('active'); $widget -> update; # try [un]*commenting this print "after delete before 2nd sleep \n"; sleep(10); print "after 2nd sleep \n"; } __END__
译文:
18.4. 如何在等待其它进程结束的过程中更新组件显示?
简单的说就是使用:
$widget -> update;
或者
$widget -> idletasks;
下面的例子中使用第一种方法。注意,这里为了演示效果,我们只是调用了Perl中的sleep()函数来实现一个“较长时间的进程”,而不是真的进行什么具体操作。
#!/usr/bin/perl -w
use Tk;
my $m = MainWindow->new();
my $l = $m -> Listbox();
$l -> bind('<Double-1>' => sub{sleepy($l)} );
my @nuts = qw(Almond Brazil Chestnut Doughnut Elmnut Filbert);
for (@nuts) { $l -> insert('end',$_); }
$l -> pack;
MainLoop;
sub sleepy {
my $widget = shift;
print "before 1st sleep \n";
sleep(10);
print "after 1st sleep before delete \n";
$widget -> delete('active');
$widget -> update; # 尝试注释掉这行,看看效果有什么不同!
print "after delete before 2nd sleep \n";
sleep(10);
print "after 2nd sleep \n";
}
__END__