in reply to Re^2: Optionmenu Variable in Name
in thread Optionmenu Variable in Name
slips thru and runs without warnings and strict enabled. This results in the entry textvariable not working as expected. It should bemy $lam_num_ent = $lam_mat_frm -> Entry(), -variable=> \$lam_num;
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:
That way, all you need to know is $num, and you can get all the information about that Optionmenu widget.my %ort_optmen; $ort_optmen{$num}{'widget'} = whatever $ort_optmen{$num}{'variable'} = whatever $ort_optmen{$num}{'options'} = whatever
So here is a basic fix to show you the way.
</c>#!/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'); } }
|
|---|