The button-1 of the listbox widget is internally bound in the c code, and hard to change behavior on. It is meant for selection. You probably will be better of using button-3 (right click) to avoid messing up things like multiple or extended selection mode.
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
my $value;
my $mw = MainWindow->new;
my $entry = $mw->Entry(
-state => 'readonly',
-textvariable => \$value
)->pack;
my $lb = $mw->Listbox( -height => 0 )->pack;
$lb->insert( 'end', qw/one two three four five six/ );
$lb->bind( '<ButtonPress-3>', [ \&setItem, \$value, Ev('@') ] );
sub setItem {
my ( $lb, $valSR, $xy ) = @_;
$lb->selectionClear( 0, 'end' );
my $index = $lb->index($xy);
if ( defined($index) ) {
$lb->selectionSet($index);
$$valSR = $lb->get($index);
}
}
MainLoop;
|