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

#!/usr/bin/perl -w use strict; use Tk; use Tk::BrowseEntry; # Create the main window my $mw; $mw->{main} = MainWindow->new; # Create the browse entry $mw->{a} = $mw->{main}->BrowseEntry(-label => "Select one:")->pack; $mw->{a}->insert("end", "one"); $mw->{a}->insert("end", "two"); $mw->{a}->insert("end", "three"); my $frame = $mw->{main}->Frame()->pack(); $frame->Button(-text => "Ok", -command => \&return_value )->pack(-side => 'left'); $frame->Button(-text => "Exit", -command => sub{ $mw->{main}->destroy } )->pack(-side +=> 'left'); MainLoop; sub return_value { #return value of selected item here }
Given that code, how would I return the value of the selected item in the list? Tried some things, but I'm still new to Tk, and this is my first attempt on Tk::BrowseEntry. Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: Tk::BrowseEntry return value
by pg (Canon) on Apr 12, 2003 at 05:41 UTC
    Modified your code a little bit:
    1. Set variable option for your BrowseEntry, and that variable will hold the selection.
    2. Completed your return_value function to print out the selection, so you can verify it.
    Tested and worked.
    use strict; use Tk; use Tk::BrowseEntry; # Create the main window my $mw; $mw->{main} = MainWindow->new; # Create the browse entry my $v; $mw->{a} = $mw->{main}->BrowseEntry(-variable => \$v, -label => "Selec +t one:")->pack; $mw->{a}->insert("end", "one"); $mw->{a}->insert("end", "two"); $mw->{a}->insert("end", "three"); my $frame = $mw->{main}->Frame()->pack(); $frame->Button(-text => "Ok", -command => \&return_value )->pack(-side => 'left'); $frame->Button(-text => "Exit", -command => sub{ $mw->{main}->destroy } )->pack(-side +=> 'left'); MainLoop; sub return_value { print "return value is $v\n"; }
Re: Tk::BrowseEntry return value
by graff (Chancellor) on Apr 12, 2003 at 05:43 UTC
    Enhance the creation of the BrowseEntry slightly, like this:
    my $browseval; $mw->{a} = $mw->{main}->BrowseEntry(-label => "Select one:", -variable => \$browseval, )->pack;
    Now, you don't need a sub to return the value -- as soon as the user selects something in the widget, that value is assigned to the scalar whose reference was passed to the "-variable" option.
      Thanks alot for the help =) Saved me alot of trouble. Works perfectly now =)