in reply to Re: perl tk listbox how to return choice to main program
in thread perl tk listbox how to return choice to main program

Thank you so much. I always think one should post the working solution once found so I have posted the working script below. I am running Xubuntu 12.04 - a Linux system

AnonyMonk's post at first seemed too minimalistic but it actually led me finally to the solution. Here is a working script that runs from the command line and opens a listbox that is contained in a subroutine, allows one to select an item and closes the listbox when selection is done with a double click returning the id of the selected item to the command line program where further actions can use the information.

#!/usr/bin/perl use strict; use Tk; my @listbox_items = qw/frodo bilbo rabbits orcs smaug gandolf/; my $choice = &myListBox(@listbox_items); print "TOP: selection is $choice\n"; sub myListBox{ my $choice; my @listbox_items = @_; my $mw = MainWindow->new; $mw->geometry('400x300'); $mw->Label(-text => "Click once to select item")->pack(); $mw->Label(-text => "Double-click selects item and closes window")- +>pack(); my $lb = $mw->Listbox(-selectmode => "single", -exportselection =>1,)->pack( ); $lb->insert('end', @listbox_items); $lb -> bind('<Button-1>'=> sub { $choice = $_[0]->get($_[0]->curselection), print "in loop selection is $choice\n";}, ); $lb -> bind('<Double-Button-1>'=> sub {$mw->destroy();} ); MainLoop; return $choice; }#endsub

One of my errors with the intitial script was not being aware of how to distinguish between single click and double click. (sigh).

I am also very grateful to those who created the thread: Tk event bindings - single vs. double click at:

node 351793