无法读取perl / tk TEXT中的文件内容

时间:2011-04-05 11:14:25

标签: perl tk

我知道这不是一个新问题..但是我尝试按照其他帖子解决问题...但是我的文本区域没有任何内容可以打印...如果我直接在我的文本上打印一个句子file..it的工作..为什么?

#!/usr/local/bin/perl
use Tk;
use File::Tail;

#Main Window
my $mw = new MainWindow;
$mw-> title ("Packet Analyzer Tool");

my $frm_1 = $mw -> Frame() -> pack();
my $frm_2 = $mw -> Frame() -> pack();
my $frm_3 = $mw -> Frame() -> pack();
my $frm_4 = $frm_3 -> Frame(-relief => 'groove', -borderwidth =>2) -> pack(-side => "left");
my $frm_5 = $frm_3 -> Frame() -> pack(-side => "right",-after => $frm_4);

my $but1 = $frm_1 -> Button(-text => "Start", 
-command =>\&push_start)
-> pack(-side => "left", -anchor => 'nw', -ipadx => 30, -ipady => 35);

my $but2 = $frm_1 -> Button(-text => "Stop",
-command =>\&push_stop)
-> pack(-side => "left",-after =>$but1, -expand => 1,-ipadx => 30, -ipady => 35);

my $but3 = $frm_1 -> Button(-text => "Pause",
-command =>\&push_pause)
-> pack(-side => "left",-after => $but2 ,-ipadx => 30, -ipady => 35);

my $but4 = $frm_1 -> Button(-text => "Exit",
-command =>\&push_exit)
-> pack(-side => "left", -after => $but3 ,-ipadx => 30, -ipady => 35);

my $filter = $frm_2 ->Entry(-width => 65) -> pack(-side =>"left",-anchor => 's');
my $but5 = $frm_2 -> Button(-text => "Search", -command =>\&push_search)
->pack(-side => "left", -after => $filter, -ipadx => 10);

my $txt1 = $frm_4 -> Text(-width => 60, -height =>20) 
-> pack(-side =>"left",-anchor => 's');
my $srl = $frm_4 -> Scrollbar(-orient=>'v', -command =>[yview => $txt]);
$txt1 -> configure(-yscrollcommand =>['set',$srl]);


$txt1 -> grid(-row=>1, -column=>1);
$srl -> grid(-row=>1, -column=>2,-sticky=>"ns");

my $txt2 = $frm_5 -> Text(-width => 15, -height =>20)
-> pack(-side=>"right", -anchor => 'e');

MainLoop;

#Executed START BUTTON
sub push_start
{
open my $fh,'<','/home/terrance/Desktop/perl/record.txt' || die $!;
my @contents = <$fh>;
foreach my $line (@contents)
{
$txt1 ->insert("end", $line);   
}


#$txt1 -> insert ('end', "hello due\n");
}

1 个答案:

答案 0 :(得分:7)

在代码上使用perltidy并添加

use strict;
use warnings;

然后你会抓住

  1. $ txt =&gt; my $srl = $frm_4->Scrollbar( -orient => 'v', -command => [ yview => $txt ] );

  2. 中的$ txt1拼写错误
  3. 缺少my @contents = <$fh>;

  4. foreach my $line (@contetns)错字

  5. 第二个想法:你确定,文件存在且可以访问吗?