http://www.perlmonks.org?node_id=980353

shortyfw06 has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code in my program and would like it if the user could enter a value in the entry box and then re-enter a different value if need be. Is there a way to alter this code to allow for re-entry? Thank you!

#!/usr/bin/perl use warnings; use strict; use Tk; use Cwd; ###################################################################### +##################### # GUI Building ###################################################################### +##################### # make the selections available to the print_input_values sub my %t; my %mat; my %ort; my %mat_lab; # Create Main Window my $mw = new MainWindow; my $lam_num; my $row=1; my $column=1; my $lam_mat_frm = $mw -> Frame(); $lam_mat_frm->grid(-row=>$row, -column=>$column,-columnspan=>6); my $lam_num_lab = $lam_mat_frm -> Label(-text=>"Input the number of pl +ies in the laminate.", -font=>"ansi 10 bold"); $lam_num_lab->grid(-row=>$row, -column=>$column); $column++; my $lam_num_ent = $lam_mat_frm -> Entry(-textvariable=> \$lam_num); $lam_num_ent->grid(-row=>$row, -column=>$column); $column++; my $lam_data_button = $lam_mat_frm->Button(-text=>"Input Laminate Data +", -command=> \&input_lam_data); $lam_data_button->grid(-row=>$row, -column=>$column); MainLoop; sub input_lam_data { $row=4; $column=2; my @lam_mat_t_lab = ( 'Material', 'Thickness', 'Orientation', ); my $n = 1; for my $label (@lam_mat_t_lab) { $mat_lab{$n} = $lam_mat_frm -> Label (-text=> $label, -font=>"ansi 8 bold"); $mat_lab{$n}->grid(-row=>$row, -column=>$column); $column=$column+1; }; my %ply_lab; $n = 1; $row=5; do { $column = 1; $ply_lab{$n} = $lam_mat_frm -> Label (-text=>"Ply $n", -font=>"ansi 8 bold"); $ply_lab{$n}->grid(-row=>$row,-column=>$column); $row = $row+1; $n = $n+1; } until ($n == $lam_num+1); # Create Material Optionmenus my @mats = ("PW","8HS","Tape"); my %mat_optmen; $n = 1; $row=5; $column=2; do { $mat_optmen{$n} = $lam_mat_frm -> Optionmenu(-options => \@mat +s, -variable => \$mat{$n}); $mat_optmen{$n}->grid(-row=>$row,-column=>$column); $row=$row+1; $n = $n + 1; } until ($n == $lam_num+1); # Create Thickness Optionmenus my @ts = ("0.0077","0.0147","0.0054"); my %t_optmen; $n = 1; $row=5; $column=3; do { $t_optmen{$n} = $lam_mat_frm -> Optionmenu(-options => \@ts, -variable => \$t{$n}); $t_optmen{$n}->grid(-row=>$row,-column=>$column); $row=$row+1; $n = $n + 1; } until ($n == $lam_num+1); # Create Orientation Optionmenus my @orts = ("-45","0","45","90"); my %ort_optmen; $n = 1; $row=5; $column=4; do { $ort_optmen{$n} = $lam_mat_frm -> Optionmenu(-options => \@ort +s, -variable => \$ort{$n}); $ort_optmen{$n}->grid(-row=>$row,-column=>$column); $row=$row+1; $n = $n + 1; } until ($n == $lam_num+1); my $print_button = $lam_mat_frm->Button(-text=>"Print BJSFM Input +File", -command=> \&print_input_file); $print_button->grid(-row=>$row,-column=>1); }

Replies are listed 'Best First'.
Re: Re-Enter Data in Perl TK
by zentara (Archbishop) on Jul 06, 2012 at 20:52 UTC
    Your code is very convoluted, and what you should do, is make the initial frame separate from the added frame, but I'm not rewriting your code. That way you can just gridForget the entire popup frame. As it is, you need to get all the gridSlaves of the Frame, pop off the widgets you need to save, and rebuild.

    But since this isn't a code writing service, here is a convoluted way to fix your convoluted code. :-) Look at the sub do_reset for what is going on, and read perldoc Tk::grid.

    This is not the best way, but it works.

    #!/usr/bin/perl use warnings; use strict; use Tk; use Cwd; ###################################################################### +##################### # GUI Building ###################################################################### +##################### # make the selections available to the print_input_values sub my %t; my %mat; my %ort; my %mat_lab; # Create Main Window my $mw = new MainWindow; my $lam_num; my $row=1; my $column=1; my $lam_mat_frm = $mw->Frame(); $lam_mat_frm->grid(-row=>$row, -column=>$column,-columnspan=>6); my $lam_num_lab = $lam_mat_frm -> Label(-text=>"Input the number of pl +ies in the laminate.", -font=>"ansi 10 bold"); $lam_num_lab->grid(-row=>$row, -column=>$column); $column++; my $lam_num_ent = $lam_mat_frm -> Entry(-textvariable=> \$lam_num); $lam_num_ent->grid(-row=>$row, -column=>$column); $column++; my $lam_data_button = $lam_mat_frm->Button(-text=>"Input Laminate Data +", -command=> \&input_lam_data); $lam_data_button->grid(-row=>$row, -column=>$column); $column++; my $reset_button = $lam_mat_frm->Button(-text=>"Reset", -command=> \&do_reset); $reset_button->grid(-row=>$row, -column=>$column); MainLoop; sub do_reset{ $lam_num_ent->delete(0,'end'); my @slaves = $lam_mat_frm->gridSlaves; print "@slaves\n"; pop @slaves for (1..4); # save the first 4 widgets print "@slaves\n"; foreach my $slave( @slaves){ $slave->gridForget; print "$slave\n"; } } sub input_lam_data { $row=4; $column=2; my @lam_mat_t_lab = ( 'Material', 'Thickness', 'Orientation', ); my $n = 1; for my $label (@lam_mat_t_lab) { $mat_lab{$n} = $lam_mat_frm -> Label (-text=> $label, -font=>"ansi 8 bold"); $mat_lab{$n}->grid(-row=>$row, -column=>$column); $column=$column+1; }; my %ply_lab; $n = 1; $row=5; do { $column = 1; $ply_lab{$n} = $lam_mat_frm -> Label (-text=>"Ply $n", -font=>"ansi 8 bold"); $ply_lab{$n}->grid(-row=>$row,-column=>$column); $row = $row+1; $n = $n+1; } until ($n == $lam_num+1); # Create Material Optionmenus my @mats = ("PW","8HS","Tape"); my %mat_optmen; $n = 1; $row=5; $column=2; do { $mat_optmen{$n} = $lam_mat_frm -> Optionmenu(-options => \@mat +s, -variable => \$mat{$n}); $mat_optmen{$n}->grid(-row=>$row,-column=>$column); $row=$row+1; $n = $n + 1; } until ($n == $lam_num+1); # Create Thickness Optionmenus my @ts = ("0.0077","0.0147","0.0054"); my %t_optmen; $n = 1; $row=5; $column=3; do { $t_optmen{$n} = $lam_mat_frm -> Optionmenu(-options => \@ts, -variable => \$t{$n}); $t_optmen{$n}->grid(-row=>$row,-column=>$column); $row=$row+1; $n = $n + 1; } until ($n == $lam_num+1); # Create Orientation Optionmenus my @orts = ("-45","0","45","90"); my %ort_optmen; $n = 1; $row=5; $column=4; do { $ort_optmen{$n} = $lam_mat_frm -> Optionmenu(-options => \@ort +s, -variable => \$ort{$n}); $ort_optmen{$n}->grid(-row=>$row,-column=>$column); $row=$row+1; $n = $n + 1; } until ($n == $lam_num+1); my $print_button = $lam_mat_frm->Button(-text=>"Print BJSFM Input +File", -command=> \&print_input_file); $print_button->grid(-row=>$row,-column=>1); }

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

      Thank you very much for your honest and helpful response. I appreciate your guidance. I do not want my code to be convoluted. I want to learn how to write Perl efficiently. I've created a seperate frame as you suggested and added a reset button with gridForget on the popup frame. Everything is cleared and the widgets disappear when reset is activated, however, when I put in a new value, the widgets do not popup a second time. If there are any other suggestions you have to make my code less convoluted, please let me know so I can make it better.

      #!/usr/bin/perl use warnings; use strict; use Tk; use Cwd; ###################################################################### +##################### # GUI Building ###################################################################### +##################### # make the selections available to the print_input_values sub my %t; my %mat; my %ort; my %mat_lab; my $scr; # Create Main Window my $mw = new MainWindow; my $lam_num; my $row=1; my $column=1; my $lam_mat_frm = $mw -> Frame(); $lam_mat_frm->grid(-row=>$row, -column=>$column,-columnspan=>6); my $lam_num_lab = $lam_mat_frm -> Label(-text=>"Input the number of pl +ies in the laminate.", -font=>"ansi 10 bold"); $lam_num_lab->grid(-row=>$row, -column=>$column); $column++; my $lam_num_ent = $lam_mat_frm -> Entry(-textvariable=> \$lam_num); $lam_num_ent->grid(-row=>$row, -column=>$column); $column++; my $lam_data_button = $lam_mat_frm->Button(-text=>"Input Laminate Data +", -command=> \&input_lam_data); $lam_data_button->grid(-row=>$row, -column=>$column); $column++; my $reset_button = $lam_mat_frm->Button(-text=>"Reset", -command=> \&do_reset); $reset_button->grid(-row=>$row, -column=>$column); $row++; my $lam_frm = $mw -> Frame(); $lam_frm->grid(-row=>$row,-column=>$column,-columnspan=>6); MainLoop; sub do_reset { $lam_num_ent->delete(0,'end'); $lam_frm->gridForget; } sub input_lam_data { $row=4; $column=2; my @lam_mat_t_lab = ( 'Material', 'Thickness', 'Orientation', ); my $n = 1; for my $label (@lam_mat_t_lab) { $mat_lab{$n} = $lam_frm -> Label (-text=> $label, -font=>"ansi 8 bold"); $mat_lab{$n}->grid(-row=>$row, -column=>$column); $column=$column+1; }; my %ply_lab; $n = 1; $row=5; do { $column = 1; $ply_lab{$n} = $lam_frm -> Label (-text=>"Ply $n", -font=>"ansi 8 bold"); $ply_lab{$n}->grid(-row=>$row,-column=>$column); $row = $row+1; $n = $n+1; } until ($n == $lam_num+1); # Create Material Optionmenus my @mats = ("PW","8HS","Tape"); my %mat_optmen; $n = 1; $row=5; $column=2; do { $mat_optmen{$n} = $lam_frm -> Optionmenu(-options => \@mats, -variable => \$mat{$n}); $mat_optmen{$n}->grid(-row=>$row,-column=>$column); $row=$row+1; $n = $n + 1; } until ($n == $lam_num+1); # Create Thickness Optionmenus my @ts = ("0.0077","0.0147","0.0054"); my %t_optmen; $n = 1; $row=5; $column=3; do { $t_optmen{$n} = $lam_frm -> Optionmenu(-options => \@ts, -variable => \$t{$n}); $t_optmen{$n}->grid(-row=>$row,-column=>$column); $row=$row+1; $n = $n + 1; } until ($n == $lam_num+1); # Create Orientation Optionmenus my @orts = ("-45","0","45","90"); my %ort_optmen; $n = 1; $row=5; $column=4; do { $ort_optmen{$n} = $lam_frm -> Optionmenu(-options => \@orts, -variable => \$ort{$n}); $ort_optmen{$n}->grid(-row=>$row,-column=>$column); $row=$row+1; $n = $n + 1; } until ($n == $lam_num+1); my $print_button = $lam_frm->Button(-text=>"Print BJSFM Input File +", -command=> \&print_input_file); $print_button->grid(-row=>$row,-column=>1); }

        I think I figured it out. Thanks! I added this to the do_reset sub.

        sub do_reset { $lam_num_ent->delete(0,'end'); $lam_frm->gridForget; $lam_frm = $mw -> Frame(); $lam_frm->grid(-row=>$row,-column=>$column,-columnspan=>6); }
Re: Re-Enter Data in Perl TK
by Anonymous Monk on Jul 06, 2012 at 19:03 UTC