I've been using Optionmenu to implement the ability of users to select a specific configuration option in the software.

I would like to control which option is the selected value (default) when the Optionmenu is first.

The current usage has me setting -textvariable to the variable I use to track the value.

I build a has to track the values that can be used in the menu (the hash values are arrays that are suboptions to drive another Optionmenu).

my %solutionMatrix; # hash of lists that contain platforms and cust +omer variants per platform my $defaultPlatform = "Mod1"; # the default platform, selected at p +rogram start $solutionMatrix{"Mod1"} = [ 'a','b','c' ]; # store the list address + in the hash for default key $solutionMatrix{"Mod2"} = [ 'q' ]; # another platform my $platformDropdown = $selectionFrame -> Optionmenu ( -textvariab +le => \$platformChoice, -indicatoron => 1 ) -> pack( -side => "left") +; # --- now populate the options in the platform and customer dr +opdown list, # which is in the hash of arrays, %solutionMatrix foreach my $platform ( sort keys %solutionMatrix ) { # $platform is a choice for the platform dropdown # @{$solutionMatrix{$platform} is an array of solutions fo +r the customer dropdown $platformDropdown -> addOptions([$platform=>$platform]); } $platformChoice = $defaultPlatform; # set to default value
The problem I'm having is that once the widget is rendered, the value of the selection appears to be the last option added to the widget. What I would like to see happen is for the selection to properly reflect the $defaultPlatform value I set the variable to. Thats not rendering however.

What I see is the widget is set up so that my $defaultPlatform is the starting selected value, however what is shown on the display screen is not that value, but the last option added.

So in the code snippet, the default is Mod1. The Optionmenu does seem to internally store that value as the selected option (based on what I see inside the callback that is executed), but the screen displays Mod2 on the widget.

Maybe its just a matter of forcing an update to the widget? If so, how is that done?


In reply to Tk Optionmenu - how to set the default selection by Minok

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.