原文:
10.12. Is there any way to have more than one Listbox contain a selection?
To allow more than one Listbox to contain a "selection", (or at least a highlighted item - which need not be the actual selection) specify the configuration option: -exportselection => 0
which will dis-associate Listbox's selection from X selection (only one window can have X selection at a time).
Here is a rather simple script that illustrates what happens when only one Listbox has -exportselection => 0 specified: #!/usr/bin/perl -w use Tk; my $main = MainWindow->new; my @fruits = ('Apple','Banana','Cherry','Date','Elderberry','Fig'); my @nuts = qw(Almond Brazil Chestnut Doughnut Elmnut Filbert); my $fruit_list = $main->Listbox(); for (@fruits) { $fruit_list -> insert('end',$_); } $fruit_list->pack(); my $fruitprint_button = $main->Button( -text => "print selection", -command => sub{ printthem($fruit_list) } )->pack; my $nut_list = $main->Listbox( -selectmode => 'multiple', -exportselection => 0, )->pack; for (@nuts) { $nut_list -> insert('end',$_); } my $nutprint_button = $main->Button( -text => "print selection(s)", -command => sub{ printthem($nut_list) } )->pack; my $quit_button = $main->Button(-text => "quit program", -command => sub{exit}, )->pack(); MainLoop; sub printthem { my $list = shift; my @entries = $list->curselection; for (@entries) { print $list -> get($_),"\n";} }
For a more extensive example of Listbox usage combined with some perl data structure exploitation see the script at: http://www.perltk.org/contrib/etc/lb-constructor
译文:
10.12. 怎样让多个Listbox可以同时被选择?
要想允许多个Listbox都可以同时被“选择”(或者至少是有高亮显示的条目——实际不一定是真正的选择项),必须指定如下的配置选项:
-exportselection => 0;
这样就可以把Listbox的选择项和X窗口的选择项分开(因为任何时候只能有一个窗口拥有X窗口的选择)。
下面是一个简单的例子,演示了当只有一个Listbox使用的-exportselection=>0时的情况:
#!/usr/bin/perl -w
use Tk;
my $main = MainWindow->new;
my @fruits = ('Apple','Banana','Cherry','Date','Elderberry','Fig');
my @nuts = qw(Almond Brazil Chestnut Doughnut Elmnut Filbert);
my $fruit_list = $main->Listbox();
for (@fruits) { $fruit_list -> insert('end',$_); }
$fruit_list->pack();
my $fruitprint_button = $main->Button(
-text => "print selection",
-command => sub{ printthem($fruit_list) }
)->pack;
my $nut_list = $main->Listbox(
-selectmode => 'multiple',
-exportselection => 0,
)->pack;
for (@nuts) { $nut_list -> insert('end',$_); }
my $nutprint_button = $main->Button(
-text => "print selection(s)",
-command => sub{ printthem($nut_list) }
)->pack;
my $quit_button = $main->Button(-text => "quit program",
-command => sub{exit},
)->pack();
MainLoop;
sub printthem {
my $list = shift;
my @entries = $list->curselection;
for (@entries) { print $list -> get($_),"\n";}
}
(译者注:建议大家尝试一下吧$nut_list定义时的-exportselection => 0,行注掉——在前面加上一个#号——再试一试,就会比较容易理解它的意义了。)