Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Optionmenu Variable in Name

by shortyfw06 (Beadle)
on Mar 08, 2012 at 19:45 UTC ( #958524=note: print w/ replies, xml ) Need Help??


in reply to Re: Optionmenu Variable in Name
in thread Optionmenu Variable in Name

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.


Comment on Re^2: Optionmenu Variable in Name
Download Code
Re^3: Optionmenu Variable in Name
by zentara (Archbishop) on Mar 09, 2012 at 13:01 UTC
    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: note [id://958524]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2014-09-23 16:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (228 votes), past polls