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

I am populating a Tk::BrowseEntry widget with a bunch of items whose index in the BrowseEntry list correspond to an element in an array. I am trying to figure out how I can get the currently selected item's index, in the Tk::BrowseEntry widget, so I can use that index number in the -browsecmd option of the Tk::BrowseEntry widget. Here is a sample:
use Tk; use Tk::BrowseEntry; my @items; my $BrowseEntryindex; my $mw = MainWindow->new(); $items[0]{'cmd'} = sub{print "First BrowseEntry Index Selected\n"}; $items[1]->{'cmd'} = sub{print "Second BrowseEntry Index Selected\n"}; #Need to set $BrowseEntryindex to the index number of the selected ite +m in $dropdown my $dropdown = $mw->BrowseEntry( -label => 'Items', -browsecmd => sub{ + $items[$BrowseEntryindex]{'cmd'}; } )->pack; $dropdown->insert('end', 'First'); $dropdown->insert('end', 'Second'); $mw->MainLoop;
  • Comment on How can I get the index of the currently selected Tk::BrowseEntry element?
  • Download Code

Replies are listed 'Best First'.
Re: How can I get the index of the currently selected Tk::BrowseEntry element?
by biohisham (Priest) on Feb 28, 2010 at 13:16 UTC
    01-Capture the user choice in a variable. 02-Check if it is in the populated array.
    Let "-browsecmd" invoke a subroutine that takes care of the second choice and checks each element in the array against the element referenced in the captured variable. This subroutine is placed outside the MainLoop for better efficiency..

    You need "-variable=>" from the BrowseEntry set of options to capture the user choice..

    use strict; use warnings; use Tk; require Tk::BrowseEntry; my @items = qw(first second third fourth); my $mw = MainWindow->new(); my $default_choice = "first"; my $dropDown = $mw->BrowseEntry( -label=>'items', -variable=>\$default_choice, #capture the user choice or show de +fault -browsecmd=>\&doIt )->pack(); $dropDown->insert('end', "First"); $dropDown->insert('end',"Second"); $dropDown->insert('end',"Third"); $mw->Button(-text=>'exit', -command=>sub{exit;})->pack(); MainLoop; #check if the array has an element identical to what's been chosen.. sub doIt{ my $x = -1; foreach my $element (@items){ $x++; print $element eq lc($default_choice)? "$default_choice index +\$items[$x]\n" : next; } }


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: How can I get the index of the currently selected Tk::BrowseEntry element?
by hangon (Deacon) on Feb 28, 2010 at 06:56 UTC

    Tk::Browsentry is a composite widget that includes several subwidgets, including a listbox to hold your selectable items. Per the docs, the listbox is an advertized subwidget named "slistbox". So, first you need to get a reference to the listbox subwidget:

    $listbox = $dropdown->Subwidget('slistbox');

    Then call the appropriate method to retreive a list of selected indexes for the listbox subwidget (see listbox widget docs):

    @indexes = $listbox->curselection;
Re: How can I get the index of the currently selected Tk::BrowseEntry element?
by Anonymous Monk on Feb 28, 2010 at 06:41 UTC
    Here you go. You need to use -browse2cmd to get the index number of the selected item.
    use Tk; use Tk::BrowseEntry; my @items; my $BrowseEntryindex; my $cur_index; my $mw = MainWindow->new(); $items[0]{'cmd'} = sub{print "First BrowseEntry Index Selected\n"}; $items[1]->{'cmd'} = sub{print "Second BrowseEntry Index Selected\n"}; my $dropdown = $mw->BrowseEntry( -label => 'Items',-browse2cmd => sub{ +&{$items[$_->{'_BE_curIndex'}]{'cmd'}};} )->pack; $dropdown->insert('end', 'First'); $dropdown->insert('end', 'Second'); $mw->MainLoop;
Re: How can I get the index of the currently selected Tk::BrowseEntry element?
by glenn (Scribe) on Jan 24, 2014 at 22:57 UTC
    Just to set the text within:
    $dropdown->Subwidget("entry")->Subwidget("entry")->configure(-text=>"F +irst");
    Use WidgetInspection to look into widgets
    use Tk; sub WidgetInspection { my $widget = $_[0]; foreach my $secret ( ['SubWidget' => "Advertised Subwidgets"], ['Delegates' => "Delegated Methods"], ['Configure' => "configure( ) Options"], ['ConfigSpecs' => "Configure Specifications"], ) { printf "\n%-11s - %s\n", $secret->[0], $secret->[1]; foreach my $class (keys %{$widget->{$secret->[0]}}) { printf "%20s: %31s\n", $class, $widget->{$secr +et->[0]}->{$class}; } } } my $mw = MainWindow->new(); my $obj = $mw->BrowseEntry(-label=>"test")->pack(); WidgetInspection($obj); MainLoop;