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

Greetings,

I'm playing around with Tk::Listbox, and am stuck on something. How do I return the selected item in the Listbox, evertime I click on said item? Everywhere I look, everyone has them set for button actions (which I don't want).

ie. when you select "dog" in the listbox, "dog" will be displayed in an Entry/Label widget. UPDATE: Please ignore my request. Was not thinking properly; used Bind to correct my problem.

Replies are listed 'Best First'.
Re: Return value in Listbox
by zentara (Cardinal) on May 13, 2009 at 16:26 UTC
    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;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku