in reply to Code after MainLoop; Tk

Any code after MainLoop; will not get executed until Tk exits. But I don't know how to make Tk exit without exiting the whole program.

An easy workaround is (should be) to put code you want executed after Tk exits in to an END block. like:

END { my $var =`crossref.pl`; ... }

However, my $var =`crossref.pl`; look like something you would want to run before MainLoop; but I don't see it referenced elsewhere in your code.

Replies are listed 'Best First'.
Re^2: Code after MainLoop; Tk
by PerlCowboy (Novice) on Jun 11, 2016 at 19:19 UTC

    I'll have to try this later today. Crossref.pl is the next script in a series of four scripts I use to extract data from a text file, then create a spreadsheet.

      Crossref.pl is the next script in a series of four scripts

      Since you are automatically starting the next script, is there a reason for not combining the scripts in to a single unified application?

      If so, a more efficient way to start the next script would be exec 'Crossref.pl';

      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`;
        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...

        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++; }