Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Tk Optionmenu - how to set the default selection

by Minok (Novice)
on Feb 09, 2009 at 20:39 UTC ( #742573=perlquestion: print w/replies, xml ) Need Help??

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

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?

Replies are listed 'Best First'.
Re: Tk Optionmenu - how to set the default selection
by zentara (Archbishop) on Feb 10, 2009 at 15:00 UTC
    Here is one way, manipulate the options array.
    #!/usr/bin/perl use warnings; use strict; use Tk; my @hrs = ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"); my $mw = new MainWindow(); my $menu = $mw->Optionmenu( -options => \@hrs, -textvariable => \$hrs[0] )->pack(); $mw->Button(-text=>'Shift',-command => sub{ push (@hrs, shift(@hrs)); $menu->configure(-options => \@hrs, -textvariable => \$hrs[0] ); $mw->update; })->pack(); #start up at desired value my $desired = 3; push (@hrs, shift(@hrs)) until $hrs[0] == 3; $menu->configure(-options => \@hrs, -textvariable => \$hrs[0] ); MainLoop;

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      Well, there-in lies the solution. Just setting the attribute of -textvariable a second time fixed the issue.

      Adding

      $platformDropdown -> configure( -textvariable => \$platformChoice );
      to the end does the trick.... which pokes the TK engine and says "Hey, set the value to THIS". Even though the variable already contained the right value, I guess TK needs to have some sort of event to know it needs to update. So the corrected code, that now works, looks like:
      my %solutionMatrix; my $defaultPlatform = "Mod1"; $solutionMatrix{"Mod1"} = [ 'a','b','c' ]; $solutionMatrix{"Mod2"} = [ 'q' ]; 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 default $platformDropdown -> configure( -textvariable => \$platformCho +ice );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://742573]
Approved by kennethk
Front-paged by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2020-07-05 20:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?