使用Perl Tk打印到文件

时间:2016-06-13 20:55:03

标签: perl file-io tk

我已经用我的代码解决了大部分问题,但现在列表框中的所有内容都会被打印到文件中,无论是否检查过。不知道为什么。任何帮助表示赞赏。我之前有过这个工作,但不知道我做了什么来改变它。我希望在选中某个项目时单击“确定”,然后单击“退出”,只有选中的项目才会被复制到文件中。

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;

  }


     }

  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 $check9 = 'NOT CHECKED';
my $check10 = 'NOT CHECKED';
my $check11 = 'NOT CHECKED';
my $check12 = 'NOT CHECKED';
my $check13 = 'NOT CHECKED';
my $check14 = 'NOT CHECKED';
my $check15 = 'NOT CHECKED';
my $check16 = '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 $job9 = "OOO";
my $chk9 = $check_frame->Checkbutton(-text => 'Return to LNI',
                                 -variable => \$check9,
                                 -onvalue => 'ADDED',
                                 -offvalue => 'NOT CHECKED')->pack();
                                 -command => print OUTFILE "$job9\n";

my $job10 = "EOSSS235";
my $chk10 = $check_frame->Checkbutton(-text => 'EOSSS235',
                                 -variable => \$check10,
                                 -onvalue => 'ADDED',
                                 -offvalue => 'NOT CHECKED')->pack(),
                                 -command => print OUTFILE "$job10\n";

my $job11 = "M4020235 20";
my $chk11 = $check_frame->Checkbutton(-text => 'M4020235',
                                 -variable => \$check11,
                                 -onvalue => 'ADDED',
                                 -offvalue => 'NOT CHECKED')->pack(),
                                 -command => print OUTFILE "$job11\n";

my $job12 = "PRTAR235";
my $chk12 = $check_frame->Checkbutton(-text => 'PRTAR235',
                                 -variable => \$check12,
                                 -onvalue => 'ADDED',
                                 -offvalue => 'NOT CHECKED')->pack();
                                 -command => print OUTFILE "$job12\n";

my $job13 = "WR115235";
my $job13b = "WR415235";
my $chk13 = $check_frame->Checkbutton(-text => 'WR115235',
                                 -variable => \$check13,
                                 -onvalue => 'ADDED',
                                 -offvalue => 'NOT CHECKED')->pack(),
                                 -command => print OUTFILE "$job13\n$job13b\n";

my $job14 = "X01RA235";
my $chk14 = $check_frame->Checkbutton(-text => 'X01RA235',
                                 -variable => \$check14,
                                 -onvalue => 'ADDED',
                                 -offvalue => 'NOT CHECKED')->pack(),
                                 -command => print OUTFILE "$job14\n";

my $job15 = "X1514235 15";
my $chk15 = $check_frame->Checkbutton(-text => 'X1514235',
                                 -variable => \$check15,
                                 -onvalue => 'ADDED',
                                 -offvalue => 'NOT CHECKED')->pack();
                                 -command => print OUTFILE "$job15\n";

my $job16 = "X4020235";
my $chk16 = $check_frame->Checkbutton(-text => 'X4020235',
                                 -variable => \$check16,
                                 -onvalue => 'ADDED',
                                 -offvalue => 'NOT CHECKED')->pack();
                                 -command => print OUTFILE "$job16\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 = "18-Letters: $check1\n07-Orders: $check2\n36-Letters: $check3\n38-Letters: $check4\n11-Orders: $check5\n99-Letters: $check6\n21-Letters: $check7\n23-Letters: $check8\nReturn L&I: $check9\nEOSSS235: $check10\nM4020235: $check11\nPRTAR235: $check12\nWR115235: $check13\nWR415235: $check13\nX01RA235: $check14\nX1514235: $check15\nX4020235: $check16";
  $mw->messageBox(-message => "Check Button Summary:\n$check_msg", -type => "ok");
}

MainLoop;

my $var =`crossref.pl`;

2 个答案:

答案 0 :(得分:2)

my $chk8 = $check_frame->Checkbutton(
    -text => '23-Letters',
    -variable => \$check8,
    -onvalue => 'ADDED',
    -offvalue => 'NOT CHECKED')->pack(),
    -command => print OUTFILE "$job8\n";

此调用的-command部分必须位于Checkbutton(…)的括号内。 (如上所述,它是一个完全独立的陈述。)

此外,您需要在sub { … }中包围命令以将其作为coderef传递,而不是立即执行并传递结果。

答案 1 :(得分:0)

主要问题似乎是右括号的错位,而不是printsub {}语句包含use strict; use warnings; use Tk; my $mw = MainWindow->new; $mw->geometry("300x500"); $mw->title("Unforecasted Jobs"); my $check_frame = $mw->Frame()->pack(-side => "top"); $check_frame->Label( -text => "Jobs That Do Not Forecast" ) ->pack(-side => "top"); my @cb_info = ( ['18L', '18-Letters'], ['07O', '07-Orders'], ['36L', '36-Letters'], ['38L', '38-Letters'], ['11O', '11-Orders'], ['99L', '99-Letters'], ['21L', '21-Letters'], ['23L', '23-Letters'], ['OOO', 'Return to LNI'], ['EOSSS235', 'EOSSS235'], ['M4020235 20', 'M4020235'], ['PRTAR235', 'PRTAR235'], ['WR115235', ['WR115235','WR415235']], ['X01RA235', 'X01RA235'], ['X1514235 15', 'X1514235'], ['X4020235', 'X4020235'] ); my @check_buttons; my @msg_info; for my $item (@cb_info) { my ( $job, $info ) = @$item; my $print_string = $info; push @check_buttons, 'NOT CHECKED'; my $cb = \$check_buttons[-1]; my $text = $info; if ( ref $info eq "ARRAY" ) { $print_string = $info->[0] . "\n" . $info->[1]; push @msg_info, [$info->[0], $cb]; push @msg_info, [$info->[1], $cb]; $text = $info->[0]; } else { push @msg_info, [$info, $cb]; } my $chk = $check_frame->Checkbutton( -text => $text, -variable => $cb, -onvalue => 'ADDED', -offvalue => 'NOT CHECKED', -command => sub { print STDOUT "$print_string\n"}, )->pack; } 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; my $newline = ''; for my $item (@msg_info) { $check_msg .= $newline . $item->[0] . ": " . ${ $item->[1] }; $newline = "\n"; } $mw->messageBox( -message => "Check Button Summary:\n$check_msg", -type => "ok" ); } MainLoop; 所解释的public static。此外,您可以通过@duskwuff使您的代码更易于管理,并使用数组作为检查按钮及其名称。例如:

public class Player {

    private String name;
    private List<Item> inventory;
    private int items;

    public Player() {
        this.inventory = new ArrayList();
    }

    public void addItem(String itemName, int itemType) {
        this.inventory.add(new Item(itemName, itemType));
    }

    public void removeItem(int x) {
        Item itemToRemove = this.inventory.get(x);
        if (itemToRemove != null) {
            this.inventory.remove(itemToRemove);
        }
    }

    public static void main(String[] args) {
        // create a new instance
        Player player = new Player();
        // call a method on the instance
        player.addItem("bla", 0);
    }
}