fcvw has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I have an application with two listboxes (lb1 and lb2. This can be used to select one item. I can identify which item is selected, but the moment I do not know how to detect which listbox is in use. On screen this is very clear. How do I detect the listbox used? I thought focusCurrent or focusLast would do the trick, but no such luck. I have a few lines of obvious code (most is cut out)
my $mw = MainWindow->new; my $lb1 = $mw->Scrolled(Listbox); my $lb2 = $mw->Scrolled(Listbox); my $b = $mw->Button(-text=>"doit", -command=>sub{&do_it}); MainLoop; sub do_it { my $i1 = $lb1->index('active'); my $i2 = $lb2->index('active'); # $i1 and $i2 are always inside 0..end ... }
Thanks for any help, F.

Replies are listed 'Best First'.
Re: selection problem with two Tk listboxes
by zentara (Cardinal) on May 02, 2005 at 20:31 UTC
    Yes, you don't explain exactly what your problem is, in terms of what you desire. Also, you don't give a working script to demonstrate your problem. From guessing at what you probably mean......

    You can't use focusCurrent(or whatever ) because you change your focus when you click the do_it button. So you have 2 choices, you can use an alternate method to launch the sub, as I show with a right-mouse click. It is usually done this way. Listbox has default bindings for the left-click, so I used the right. Also you want to check out how the -exportselection works, when you have a couple of listboxes.

    Now if you want to keep the do_it button, you will have to save which listbox was last clicked in a variable.

    #!/usr/bin/perl use warnings; use strict; use Tk; my $lastactive = ''; my $mw = MainWindow->new; my $lb1 = $mw->Scrolled('Listbox', -exportselection => 0, )->pack; my $lb2 = $mw->Scrolled('Listbox', -exportselection => 0, )->pack; my $b = $mw->Button(-text=>"doit", -command=>sub{&do_it1})->pack; for(0..10){ $lb1->insert($_,$_); $lb2->insert($_,$_); } $lb1->bind( '<ButtonPress-1>', sub{$lastactive = 1} ); $lb2->bind( '<ButtonPress-1>', sub{$lastactive = 2} ); $lb1->bind( '<ButtonPress-3>', [\&do_it, 1 ] ); $lb2->bind( '<ButtonPress-3>', [\&do_it, 2 ] ); MainLoop; ################################################################ sub do_it { my( $object, $selbox ) = @_; my $i1 = $lb1->index('active'); my $i2 = $lb2->index('active'); print "selbox->$selbox lastactive->$lastactive $i1 $i2 \n"; } ################################################################ sub do_it1 { my $i1 = $lb1->index('active'); my $i2 = $lb2->index('active'); print "lastactive->$lastactive $i1 $i2 \n"; }

    I'm not really a human, but I play one on earth. flash japh
Re: selection problem with two Tk listboxes
by graff (Chancellor) on May 03, 2005 at 02:33 UTC
    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"; } }
      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.
Re: selection problem with two Tk listboxes
by sasikumar (Monk) on May 02, 2005 at 19:32 UTC
    Hey
    Your question is little bit wage but still

    At the moment I do not know how to detect which listbox is in use.

    What do u mean by this? If that means list box selection then use a object which will have the instance of the valid list box.

    Update:You can select the listbox based on the mouse click(I hope the last click in either of the list box is the active one)

    Thanks
    SasiKumar