G'day bdalzell,

I tried the code you posted but it didn't compile. I added a closing brace (}) after MainLoop;: it now compiled but didn't run as you described. I tried a few other things to fix the code you provided but, ultimately, I gave up on it.

Luckily, I already had a script using Tk::Listbox which wasn't too dissimilar to the look-and-feel of your GUI. I used a temporary file to capture the user selection and forked a process to run the GUI. This seems to provide the functionality you're after. [Portability is potentially an issue: see fork, perlport - fork and perlfork for caveats.]

#!/usr/bin/env perl use strict; use warnings; use autodie qw{:all}; use Time::HiRes; use Tk; use Tk::LabFrame; print 'Hit enter when ready to make selection: '; (undef) = scalar <>; my $no_selection_msg = 'No item selected'; my $filename = join '.' => Time::HiRes::time, $$, 'tmp'; open my $pre_fork_fh, ">", $filename; print $pre_fork_fh $no_selection_msg; close $pre_fork_fh; my $pid = fork; if ($pid == 0) { my $mw = MainWindow->new; $mw->geometry('200x300+50+50'); my $app_F = $mw->Frame(); $app_F->pack(-padx => 10, -pady => 10, -fill => 'both', -expand => + 1); my $command_LF = $app_F->LabFrame( -label => 'Commands', -labelside => 'acrosstop' )->pack(-side => 'bottom', -fill => 'x', -expand => 0); my $selections_LF = $app_F->LabFrame( -label => 'Selections', -labelside => 'acrosstop' )->pack(-side => 'top', -fill => 'both', -expand => 1); my $selections_Lb = $selections_LF->Scrolled(Listbox => -scrollbars => 'osoe', -selectmode => 'single' )->pack(-padx => 5, -pady => 5, -fill => 'both', -expand => 1); $selections_Lb->insert(end => map { "$_ selected" } 'A' .. 'Z'); my $save_B = $command_LF->Button( -text => 'Save Selection', -command => sub { my $selection_index = $selections_Lb->curselection(); my $selection = length $selection_index ? $selections_Lb->get($selection_index) : $no_selection_msg; open my $child_fork_fh, ">", $filename; print $child_fork_fh $selection; close $child_fork_fh; exit; } )->pack(-padx => 5, -pady => 5); MainLoop; exit; } waitpid $pid => 0; open my $post_fork_fh, "<", $filename; my $selection = <$post_fork_fh>; close $post_fork_fh; unlink $filename; print "Selection made: $selection\n";

When you run this, you'll start with a commandline prompt and no GUI. Hitting <Return> brings up the GUI and allows a selection to be made. Once selected, the GUI shuts down and you're back to the commandline with the selection displayed. It should be reasonably idiot-proof inasmuch as it cleanly handles users not making a selection or finding interesting ways to kill the GUI without actually interacting with it.

-- Ken


In reply to Re: perl tk listbox how to return choice to main program by kcott
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.