9.11. 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://w4.lns.cornell.edu/~pvhp/ptk/etc/lb-constructor

Previous | Return to table of contents | Next