in reply to Optionmenu Variable in Name

I am trying to enable a specific number of optionmenus based on an entry variable.

Hi. First off, always try to make a complete working example, so we don't have to flesh out your code to test it ourselves.

I'm not sure if you want 3 Optionmenus, with only 1 active, OR you want a single Optionmenu, in which you can change it's options dynamically based on the Entry callback. You might look at Setting options in Tk::Optionmenu without the callback?

I don't know if you are aware of it, but the Optionmenu widget's "options" array setting can be dynamically changed. A simple example:

#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); my @opt1 = ('a'..'m'); my @opt2 = ('n'.. 'z'); my $var = 'a'; my $tvar = 'a'; my $opt = $mw->Optionmenu( -command => \&show_choice, -variable => \$var, -textvariable => \$tvar, -options => \@opt1, )->pack; $mw->Button(-text=>'Change Options',-command=>[\&change_ops, $opt])->p +ack; $mw->Button(-text=>'Exit', -command=>sub{$mw->destroy})->pack; MainLoop; sub show_choice { print "got: ", shift, "\n" } sub change_ops { my $op = shift; $tvar = 'n'; $op->configure( -options => \@opt2 ); }
If you really want to have a bunch of disabled Optionmenus laying around, it's your choice, but post a more complete example.

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Optionmenu Variable in Name
by shortyfw06 (Beadle) on Mar 08, 2012 at 19:45 UTC

    Thank you for the response. I am trying to create 3 different option menus. In the big picture, there will be a maximum number of optionmenus available but all disabled. When the user types in the entry, in this case, 1 2 or 3, that number of optionmenus are enabled. Here is a working code. The sub input_lam_data is where I'd like to replace the three configure commands with a loop.

    use Tk; # Variables my $lam_num; my $ort1; my $ort2; my $ort3; # Main Window my $mw = new MainWindow; # Build GUI my $lam_mat_frm = $mw -> Frame(); my $lam_num_ent = $lam_mat_frm -> Entry(), -variable=> \$lam_num; my $ort_optmen1 = $lam_mat_frm -> Optionmenu(-options => [qw(-45 0 45 +90)], -variable => \$ort1); my $ort_optmen2 = $lam_mat_frm -> Optionmenu(-options => [qw(-45 0 45 +90)], -variable => \$ort2); my $ort_optmen3 = $lam_mat_frm -> Optionmenu(-options => [qw(-45 0 45 +90)], -variable => \$ort3); $ort_optmen1 -> configure(-state => 'disabled'); $ort_optmen2 -> configure(-state => 'disabled'); $ort_optmen3 -> configure(-state => 'disabled'); my $lam_data_button = $lam_mat_frm->Button(-text=>"Input Laminate Data +", -command=> \&input_lam_data); # Geometry Management $lam_mat_frm -> grid(-row=>1, -column=>1, -columnspan=>2); $lam_num_ent -> grid(-row=>1,-column=>1); $ort_optmen1 -> grid(-row=>2,-column=>1); $ort_optmen2 -> grid(-row=>3,-column=>1); $ort_optmen3 -> grid(-row=>4,-column=>1); $lam_data_button -> grid(-row=>5,-column=>1); MainLoop; sub input_lam_data { $ort_optmen1 -> configure(-state => 'normal'); $ort_optmen2 -> configure(-state => 'normal'); $ort_optmen3 -> configure(-state => 'normal'); }

    Thank you.

      First off, your code had a sneaky error in which resulted from you not using warnings and strict. Specifically
      my $lam_num_ent = $lam_mat_frm -> Entry(), -variable=> \$lam_num;
      slips thru and runs without warnings and strict enabled. This results in the entry textvariable not working as expected. It should be
      my $lam_num_ent = $lam_mat_frm -> Entry(-textvariable=> \$lam_num);

      To solve the "variable in name" problem, use hashes. In the code below, I only put the Optionmenu widgets in a hash, but for a decent program, you should put all information related to each Optionmenu into a hash, like this for example:

      my %ort_optmen; $ort_optmen{$num}{'widget'} = whatever $ort_optmen{$num}{'variable'} = whatever $ort_optmen{$num}{'options'} = whatever
      That way, all you need to know is $num, and you can get all the information about that Optionmenu widget.

      So here is a basic fix to show you the way.

      #!/usr/bin/perl use warnings; use strict; use Tk; # Variables my $lam_num; my $ort1; my $ort2; my $ort3; # Main Window my $mw = new MainWindow; # Build GUI my $lam_mat_frm = $mw -> Frame(); my $lam_num_ent = $lam_mat_frm -> Entry(-textvariable=> \$lam_num); my %ort_optmen; #create hash to hold the optionmenu widgets $ort_optmen{1} = $lam_mat_frm -> Optionmenu(-options => [qw(-45 0 45 9 +0)], -variable => \$ort1); $ort_optmen{2} = $lam_mat_frm -> Optionmenu(-options => [qw(-45 0 45 9 +0)], -variable => \$ort2); $ort_optmen{3} = $lam_mat_frm -> Optionmenu(-options => [qw(-45 0 45 9 +0)], -variable => \$ort3); for(1..3){ $ort_optmen{$_} -> configure(-state => 'disabled'); } my $lam_data_button = $lam_mat_frm->Button(-text=>"Input Laminate Data +", -command=> \&input_lam_data); # Geometry Management $lam_mat_frm -> grid(-row=>1, -column=>1, -columnspan=>2); $lam_num_ent -> grid(-row=>1,-column=>1); $ort_optmen{1} -> grid(-row=>2,-column=>1); $ort_optmen{2} -> grid(-row=>3,-column=>1); $ort_optmen{3} -> grid(-row=>4,-column=>1); $lam_data_button -> grid(-row=>5,-column=>1); MainLoop; sub input_lam_data { print "$lam_num\n"; #reset them all off to begin with foreach (1..3){ $ort_optmen{$_} -> configure(-state => 'disabled'); } # you need to do some valid range checking for $lam_num here # I leave that to you foreach my $num (1..$lam_num){ $ort_optmen{$num} -> configure(-state => 'normal'); } }
      </c>

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh