in reply to selection problem with two Tk listboxes

If I understand your question, you are using the simplest, default configuration for your two listboxes, which means that when you select an item in one box, the current selection in the other box goes away; and you want to know which listbox has a currently selected item. Just check which box has a non-empty "curselection".

Here is a really simple-minded extension of the code you posted, which should demonstrate what you're looking for:

#!/usr/bin/perl use strict; use Tk; my @list1 = qw/one two three four five six seven eight nine/; my @list2 = qw/green blue red yellow orange black brown white purple/; my $list_used; my $mw = MainWindow->new; my $lb1 = $mw->Scrolled('Listbox')->pack; my $lb2 = $mw->Scrolled('Listbox')->pack; my $b = $mw->Button(-text => "do it", -command => \&do_it)->pack; $lb1->insert( 'end', @list1 ); $lb2->insert( 'end', @list2 ); MainLoop; sub do_it { if ( $lb1->curselection ) { print " got item from lb1: ",$lb1->get($lb1->curselection),$/; } elsif ( $lb2->curselection ) { print " got item from lb2: ",$lb2->get($lb2->curselection),$/ +; } else { print "Nothing was selected\n"; } }

Replies are listed 'Best First'.
Re^2: selection problem with two Tk listboxes
by fcvw (Novice) on May 03, 2005 at 06:25 UTC
    Asking a question is harder than I hoped. The solution above solved my problem; curselection was the magic word. It has only one problem: If the list has 1 item
     if ( $lb1->curselection ) {
    will be false. I changed it to
     if ($lb1->curselection)>=0 ) {
    but this generates an error message when the listbox is not selected. For the rest it works fine.

    Thanks for all the help, F.