in reply to Re^2: Code after MainLoop; Tk
in thread Code after MainLoop; Tk

Thanks for the help. Using $mw->destroy worked in exiting the MainLoop. However, I have a different issue now. Somehow, after I select my items and exit the menu, all the items listed get added regardless if I check them or not. Here's the code I'm using:

use strict; use warnings; use Tk; open(INFILE,"d1528235.txt") or die "could not open file for reading!\n +"; open(OUTFILE, '>forecast.txt') or die "Cannot open forecast.txt: $!"; while(<INFILE>){ if (m/^\s*$/) { next; } chomp $_; my @fields = split(/\ /,$_); my @output; foreach my $field(@fields){ if($field =~ /^\*?[ABMQRWY][A-Z0-9]{4}235 / ){ push @output,$field; } # print OUTFILE "$_\n"; } if (@output) { my $line = join('',@output); print "$line\n"; print OUTFILE "$line\n"; } } my $mw = MainWindow->new; $mw->geometry("300x500"); $mw->title("Unforecasted Jobs"); my $check1 = 'NOT CHECKED'; my $check2 = 'NOT CHECKED'; my $check3 = 'NOT CHECKED'; my $check4 = 'NOT CHECKED'; my $check5 = 'NOT CHECKED'; my $check6 = 'NOT CHECKED'; my $check7 = 'NOT CHECKED'; my $check8 = 'NOT CHECKED'; my $check_frame = $mw->Frame()->pack(-side => "top"); $check_frame->Label(-text=>"Jobs That Do Not Forecast")->pack(-side => + "top")->pack(); my $job = "18L"; my $chk1 = $check_frame->Checkbutton(-text => '18-Letters', -variable => \$check1, -onvalue => ' ADDED', -offvalue => 'NOT CHECKED')->pack +(), -command => print OUTFILE "$job\n +"; my $job2 = "07O"; my $chk2 = $check_frame->Checkbutton(-text => '07-Orders', -variable => \$check2, -onvalue => ' ADDED', -offvalue => 'NOT CHECKED')->pack +(), -command => print OUTFILE "$job2\ +n"; my $job3 = "36L"; my $chk3 = $check_frame->Checkbutton(-text => '36-Letters', -variable => \$check3, -onvalue => ' ADDED', -offvalue => 'NOT CHECKED')->pack +(); -command => print OUTFILE "$job3\ +n"; my $job4 = "38L"; my $chk4 = $check_frame->Checkbutton(-text => '38-Letters', -variable => \$check4, -onvalue => ' ADDED', -offvalue => 'NOT CHECKED')->pack +(), -command => print OUTFILE "$job4\ +n"; my $job5 = "11O"; my $chk5 = $check_frame->Checkbutton(-text => '11-Orders', -variable => \$check5, -onvalue => ' ADDED', -offvalue => 'NOT CHECKED')->pack +(), -command => print OUTFILE "$job5\ +n"; my $job6 = "99L"; my $chk6 = $check_frame->Checkbutton(-text => '99-Letters', -variable => \$check6, -onvalue => ' ADDED', -offvalue => 'NOT CHECKED')->pack +(); -command => print OUTFILE "$job6\ +n"; my $job7 = "21L"; my $chk7 = $check_frame->Checkbutton(-text => '21-Letters', -variable => \$check7, -onvalue => ' ADDED', -offvalue => 'NOT CHECKED')->pack +(), -command => print OUTFILE "$job7\ +n"; my $job8 = "23L"; my $chk8 = $check_frame->Checkbutton(-text => '23-Letters', -variable => \$check8, -onvalue => ' ADDED', -offvalue => 'NOT CHECKED')->pack +(), -command => print OUTFILE "$job8\ +n"; my $button_frame = $mw->Frame()->pack(-side => "bottom"); my $ok_button = $button_frame->Button(-text => 'OK', -command => \&check_sub)->pack( +-side => "left"); my $exit_button = $button_frame->Button(-text => 'Exit', -command => sub{$mw->destroy}) +->pack(-side => "right"); sub check_sub { my $check_msg = "Check #1: $check1\nCheck #2: $check2\nCheck #3: $ch +eck3\nCheck #4: $check4\nCheck #5: $check5\nCheck #6: $check6\nCheck +#7: $check7\nCheck #8: $check8\nCheck #9: $check9\nCheck #10: $check1 +0\nCheck #11: $check11\nCheck #12: $check12\nCheck #13: $check13\nChe +ck #14: $check14\n,Check #15: $check15\nCheck #16 $check16"; $mw->messageBox(-message => "Check Button Summary:\n$check_msg", -ty +pe => "ok"); } MainLoop; my $var =`crossref.pl`;

Replies are listed 'Best First'.
Re^4: Code after MainLoop; Tk
by soonix (Chancellor) on Jun 13, 2016 at 21:36 UTC
    Can't test at the moment, but shouldn't these
    -command => print OUTFILE "$job8\n";
    instead be
    -command => sub {print OUTFILE "$job8\n"};
    Otherwise the print is done when constructing the buttons instead of when pressing them...
      You are right. This fixed that issue. Thank you! My code is now fully functional! :)
Re^4: Code after MainLoop; Tk
by RonW (Parson) on Jun 13, 2016 at 20:28 UTC

    As I have recently gotten a new PC at work, I have not yet installed all of the various tools I use, so I don't have Tk handy to try your code. However...

    In your check_sub subroutine, at the time of this reply, all of your checks will be listed. Are the values of $check1 though $check16 what you expected?

    I did notice that you have:

    -offvalue => 'NOT CHECKED')->pack(), -command => print OUTFILE "$job8\n";

    It should be:

    -offvalue => 'NOT CHECKED', -command => print OUTFILE "$job8\n")->pack();

    Also, you could use arrays in your code. Something like (not tested):

    my @jobs = qw( 18L 07O and so on ); my @checks = ('NOT CHECKED') x @jobs; my @buttons; my $i = 0; for (@jobs) { my $buttons[$i] = $check_frame->Checkbutton ( -text => $_, -variable => \$checks[$i], -onvalue => ' ADDED', -offvalue => 'NOT CHECKED', -command => print OUTFILE "$jobs[$i]\n" )->pack(); $i++; }
      Ron, with your help and Soonix, my code is now fully fundtional. The command line was changed to: -command => sub {print OUTFILE "$job17\n"})->pack();

      This got the effect I was looking for in that only the checked items are sent accross to the spreadsheet. Thanks!