in reply to Tk Optionmenu - how to set the default selection

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

Replies are listed 'Best First'.
Re^2: Tk Optionmenu - how to set the default selection
by Minok (Novice) on Feb 10, 2009 at 18:29 UTC
    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 );