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?

In reply to TK Native Option Menu - Options Update Problems by merrymonk

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.