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

In reply to Re^2: perl tk listbox how to return choice to main program by bdalzell
in thread perl tk listbox how to return choice to main program by bdalzell

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.