Dear Monks, I am trying to make a listbox with a scroll bar that returns only one value back to the main function. This value will further be used in a different part of the program. 1) My script below is not able to return a value and store it $choice 2) How can I modify this script to allow only one item to be selected. i.e When the user selects one option and hits view, the choice should be returned to the main function and the listbox should exit. Any help would be highly appreciated. Thanks!
#!/usr/bin/perl use strict; use warnings; use Tk; my @choices = qw/Apple Ball Cat Dog Elephant Fish Goat House Ink Jug K +ite/; my $choice = &myListBox(@choices); print "Selection is $choice\n"; sub myListBox{ my $choice; my @listbox_items = @_; my $mw = MainWindow->new; $mw->title("Listbox"); my $lb = $mw->Scrolled("Listbox", -scrollbars => "osoe", -height => 10, -width => 30, -selectmode => "single"); my $real_lb = $lb->Subwidget('scrolled'); $real_lb->configure(-borderwidth=>0); $lb->insert("end", @listbox_items); $lb->pack(-side => "left"); $mw->Button(-text => "Exit", -command => sub{exit; })->pack(-side => "bottom"); $mw->Button(-text=>"View", -command => sub { $choice= $lb->get('active'); my $w = $real_lb->width; my $sample = ' ' x 10; my $font_len = $lb->fontMeasure('default', $sample ); } )->pack(-side => "bottom"); $lb->selectionSet('end'); $lb->see('end'); MainLoop; return $choice; }

In reply to Returning a value from a Perl Listbox by Ppeoc

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.