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

I am trying to update the options of a native option menu by replacing the existing options with new ones.
I created the native option menu using the details given in Mastering Perl Tk.
The subroutine to create the native option menu and its options is next (taken from the reference)
sub native_optionmenu { my($parent, $varref, $command, @optionvals) = @_; $$varref = $optionvals[0]; my $mb = $parent->Menubutton( -textvariable => $varref, -indicatoron => 1, -relief => 'raised', -borderwidth => 2, -highlightthickness => 2, -anchor => 'c', -direction => 'below', ); my $menu = $mb->Menu(-tearoff => 0); $mb->configure(-menu => $menu); my $callback = ref($command) =~ /CODE/ ? [$command] : $command; foreach (@optionvals) { $menu->radiobutton( -label => $_, -variable => $varref, -command => [@$callback, $_], ); } $mb; }
Next is the subroutine I created, based on the one above, to alter the options.
The main changes are that I give the name of the native option menu create rather than its parent
and there is a reference to the array of the new options I want.
sub update_native_optionmenu_options { my($native_nm, $varref, $command, $ref_optionvals) = @_; my ($menu, $callback); $$varref = $ref_optionvals->[0]; $callback = ref($command) =~ /CODE/ ? [$command] : $command; foreach (@$ref_optionvals) { $menu = $native_nm->cget(-menu); $menu->radiobutton( -label => $_, -variable => $varref, -command => [@$callback, $_], ); } }
I call this new subroutine with the following.
update_native_optionmenu_options( $nm_wg, \$nm_string, [sub {print "native om = @_.\n"}, 'First'], \@nm_local_options);
I have two problems.
The new options are added to the existing options rather than replacing them (and if I call the update subroutine
again the list of options can even longer).
I can only ‘select’ one of the original options even though I have moved the cursor over one of the new
options and pressed the left mouse button.
How can I overcome these issues?

Replies are listed 'Best First'.
Re: TK Native Option Menu - Options Update Problems
by zentara (Cardinal) on Nov 22, 2008 at 17:43 UTC
    Without a working code snippet, it is hard for me to divine what you really are trying to do......but I think you are looking for a way to switch array references in the optionmenu. Look at this example, and see if it yields a clue, all you need to do is figure out that AoAoA structure, and manipulate it. Also see Setting options in Tk::Optionmenu without the callback?
    #!/usr/bin/perl =head1 In article <1177961957.521777.74770@l77g2000hsb.googlegroups.com>, ken +@shail.co.uk says... > I am using an optionMenu to select the native language. > I therefore need to have the text in the -option array/list change > when it has been used to select a new language. > For example if "German" is selected that needs to change to "Deutch" +. > Only the original array gets used. > I have tried using a scalar to house the option array reference and > then pointing it to a new array for the new language but the origina +l > reference seems to be retained. Does the following do what you want? =cut use strict; use warnings; use Tk; my $reconfiguring; my %configuration = ( language => 0, ); my $mw = tkinit; my @languages = ( [[English => 0], [German => 1], [French => 2]], [[Englisch => 0], [Deutch => 1], [Franzoesisch => 2]], [[Anglais => 0], [Allemand => 1], [Francais => 2]], ); my $om = $mw->Optionmenu( -variable => \$configuration{language}, -options => $languages[$configuration{language}], )->pack; $om->configure(-command => sub{reconfigure($_[0])}); MainLoop; sub reconfigure { my($language) = @_; if (! $reconfiguring) { $reconfiguring = 1; $om->configure(-options => $languages[$language]); $reconfiguring = 0; } } __END__

    I'm not really a human, but I play one on earth Remember How Lucky You Are