Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Optionmenu Variable in Name

by shortyfw06 (Beadle)
on Mar 08, 2012 at 17:16 UTC ( #958498=perlquestion: print w/ replies, xml ) Need Help??
shortyfw06 has asked for the wisdom of the Perl Monks concerning the following question:

This is my first perl code so be as critical as you like so I don't form any bad habits. I am trying to enable a specific number of optionmenus based on an entry variable. Here is the code.

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 = $button_frm->Button(-text=>"Input Laminate Data" +, -command=> \&input_lam_data); sub input_lam_data { $n = 1; do { my @ort_optmen; $ort_optmen[$n] -> configure(-state => 'normal'); #This line is th +e issue $n = $n+1; } until ($n == $lam_num+1); }

Thank you for the help.

Comment on Optionmenu Variable in Name
Download Code
Re: Optionmenu Variable in Name
by zentara (Archbishop) on Mar 08, 2012 at 18:07 UTC
    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

      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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-07-29 11:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (216 votes), past polls