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"; }
In reply to Re: selection problem with two Tk listboxes
by zentara
in thread selection problem with two Tk listboxes
by fcvw
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |