使用 Perl/Tk 的简单前端






2.89/5 (9投票s)
2004 年 2 月 5 日
1分钟阅读

40870

480
使用 Perl/Tk 的简单前端
引言
本文展示了如何使用 Perl 编写一个简单的图形用户界面。
安装
请从 ActiveState 下载页面 安装最新版本的 Perl 和 Tk。
Using the Code
我们首先声明 MainWindow 对象并配置对象属性。
# Create the main window
my $x_win = MainWindow->new();
$x_win->configure(-title=>'Test');
$x_win->geometry('+200+100');
该应用程序分为四个框架:顶部一个 (personInfo),中间一个 (midFrame),以及底部两个用于“加载和保存数据”按钮。personInfo 分为左侧框架用于标签,右侧框架用于字段。
my $personInfo = $x_win->Frame(-background=>'cyan')->pack(-side=>'top',
                   -fill=>'x');
my $personLeft = $personInfo->Frame(-background=>'cyan')->pack(-side=>'left',
                   -fill=>'x');
my $personRight = $personInfo->Frame(-background=>'cyan')->pack(-side=>'left',
                   -fill=>'x',-padx=>20,-pady=>20);
# Labels
$personLeft->Label(-text=>'Personal Information',-background=>'cyan')->pack();
$personLeft->Label(-text=>'',-background=>'cyan')->pack();
$personLeft->Label(-text=>'Name',-background=>'cyan')->pack();
$personLeft->Label(-text=>'Age',-background=>'cyan')->pack();
$personLeft->Label(-text=>'Address',-background=>'cyan')->pack();
$personLeft->Label(-text=>'Occupation',-background=>'cyan')->pack();
变量绑定到 midFrame 框架中的标签。这意味着当标签控件的状态发生变化时,变量会更新以反映新的状态,反之亦然。
# Fields
$personRight->Label(-text=>'',-background=>'cyan')->pack();
$personRight->Label(-text=>'',-background=>'cyan')->pack();
$personRight->Entry(-background=>'green',-width=>20,-borderwidth=>2,
    -relief=>'sunken',-textvariable=>\$perInfo->{NAME})->pack();
$personRight->Entry(-background=>'green',-width=>20,-borderwidth=>2,
    -relief=>'sunken',-textvariable=>\$perInfo->{AGE})->pack();
$personRight->Entry(-background=>'green',-width=>20,-borderwidth=>2,
    -relief=>'sunken',-textvariable=>\$perInfo->{ADDR})->pack();
$personRight->Entry(-background=>'green',-width=>20,-borderwidth=>2,
    -relief=>'sunken',-textvariable=>\$perInfo->{OCCUPATION})->pack();
midFrame 用于在“个人信息”和“加载和保存数据”按钮之间放置某种分隔符。
# Separator Bar
my $midFrame = $x_win->Frame(-borderwidth=>3,-relief=>'groove',
                             -background=>'purple',
)->pack(-side=>'top');
$midFrame->Label(width=>100,height=>0,-foreground=>'white',
-background=>'purple')->pack();
# Process frame
my $saveFrame = $x_win->Frame(-background=>'cyan')->pack(-side=>'top',
                              -fill=>'x');
$saveFrame->Button(-text => 'Save Data',-command => \&saveinfo,
              )->grid(qw/-row 2 -column 0 -sticky nesw/);
# Process frame
my $loadFrame = $x_win->Frame(-background=>'cyan')->pack(-side=>'top',
                              -fill=>'x');
$loadFrame->Button(-text => 'Load Data',-command => \&loadinfo,
              )->grid(qw/-row 2 -column 0 -sticky nesw/);
MainLoop() 信号着 TK 部件构建的结束,后面的代码将是子例程。saveInfo 转储 perInfo 哈希的状态,loadInfo 执行相反的操作。请注意,perInfo 绑定到 personRight 框架中的控件。
MainLoop();
sub saveinfo
{
    open(OUT,">test.txt");
    print OUT "Name" . "=" . $perInfo->{NAME} . "<br>\n";
    print OUT "Age" .  "=" . $perInfo->{AGE} . "<br>\n";
    print OUT "Address" .  "=" . $perInfo->{ADDR} . "<br>\n";
    print OUT "Occupation:" . "=" . $perInfo->{OCCUPATION} . "<br>\n";
    close(OUT);
    $file = $x_win->getSaveFile(-filetypes => \@types);
    open(OUT,">$file");
    #print OUT "Content-type:text/html\n\n";
    print OUT $perInfo->{NAME} . "\n";
    print OUT $perInfo->{AGE} . "\n";
    print OUT $perInfo->{ADDR} . "\n";
    print OUT $perInfo->{OCCUPATION} . "\n";
    close(OUT);
}
sub loadinfo
{
    my $file;
    
    # Types are listed in the dialog widget
    my @types = (["Config Files", '.txt', 'TEXT'],
               ["All Files", "*"] );
    $file = $x_win->getOpenFile(-filetypes => \@types);
    open IN,$file;
    my @tfile = <IN>;
    chop @tfile;
    close (IN);
    $perInfo->{NAME} = $tfile[0];
    $perInfo->{AGE} = $tfile[1];
    $perInfo->{ADDR} = $tfile[2];
    $perInfo->{OCCUPATION} = $tfile[3];
}
本文介绍了安装 Perl 解释器和使用 Tk 编写可视化应用程序的基础知识。我鼓励您在网上进一步探索,并尝试一些简单的程序。
许可证
本文未附加明确的许可证,但可能在文章文本或下载文件本身中包含使用条款。如有疑问,请通过下面的讨论区联系作者。
作者可能使用的许可证列表可以在此处找到。
