原文:
12.8. How do I ring the bell?
The short answer is $widget -> bell;
A slightly longer answer might include a fully functioning script: #!/usr/bin/perl use Tk; $main = MainWindow -> new; $butn = $main->Button(-text => 'bell') $butn->configure(-command => sub{ $butn->bell; }); $butn->pack(); MainLoop;
An even longer answer would be a fully functioning script with a callback: #!/usr/bin/perl use Tk; $main = MainWindow -> new; $but = $main->Button(-text => 'bell', -command => sub{ringit($main)})->pack; MainLoop; sub ringit { my $m = shift; $m->bell; }
Simon Galton mailto:galtons@candu.aecl.ca reminds us to be careful in that
some systems remap this [the "console bell"] to anything from a digital sound to a flash on the screen.
译文:
12.8 如何在程序中鸣笛?
简单的说就是使用下面的方法:
$widget -> bell;
下面的脚本包含了一个完整的子程序,可以作为一个简洁而全面的答案:
#!/usr/bin/perl
use Tk;
$main = MainWindow -> new;
$butn = $main->Button(-text => 'bell');
$butn->configure(-command => sub{ $butn->bell; });
$butn->pack();
MainLoop;
作为更加全面的回答,下面的例子中还包含了一个实名子程序作为回应函数:
#!/usr/bin/perl
use Tk;
$main = MainWindow -> new;
$but = $main->Button(-text => 'bell',
-command => sub{ringit($main)})->pack;
MainLoop;
sub ringit {
my $m = shift;
$m->bell;
}
Simon Galton提醒我们注意,在有些系统中,这种控制台的鸣笛操作可能被诸如一个数字化的声音(?)或甚至是一些屏幕输出所取代。