了解 Perl/Tk 模块--Perl/Tk 基础知识

http://tech.ddvip.com   2007年11月20日    社区交流

内容摘要:尽管基于 Perl 的 Web 接口存在局限性,但 Perl 仍是最常用的 Web 开发语言之一。在 Shell 脚本、Perl 或其他语言方面有编程经验的 UNIX? 用户可通过使用 Perl/Tk 模块为基于 Perl 的 Web 接口带来新的生机。

  但是,正如该示例所示,您仅能提供一行文本。

  按钮

  当按钮小部件被最终用户激活后,可创建一个可执行函数或命令的按钮。几乎所有的图形程序都包括按钮,因此,可以非常方便地熟悉按钮小部件。

  以下示例使用三个按钮。第一个按钮标记为 Button 1,显示消息 Button 1 pushed 和“OK”按钮。第二个按钮标记为 Button 2,显示消息 Button 2 pushed 和“Yes”与“No”按钮,询问用户是否希望退出程序。根据所单击的按钮,将显示一条新消息,指示单击了“Yes”还是单击了“No”按钮,程序退出还是不退出。

  以下是一个示例脚本:

#!/usr/bin/perl -w
use Tk;
use strict;
my $mw = MainWindow->new;
$mw->geometry("200x100");
$mw->title("Button Test");
my $button1 = $mw->Button(-text => "Button #1", -command => &button1_sub)->pack();
my $button2 = $mw->Button(-text => "Button #2", -command => &button2_sub)->pack();
sub button1_sub {
 $mw->messageBox(-message => "Button 1 Pushed", -type => "ok");
}
sub button2_sub {
 my $yesno_button = $mw->messageBox(-message => "Button 2 Pushed. Exit?",
                    -type => "yesno", -icon => "question");
 $mw->messageBox(-message => "You pressed $yesno_button!", -type => "ok");
 if ($yesno_button eq "Yes") {
  $mw->messageBox(-message => "Ok, Exiting.", -type => "ok");
  exit;
 } else {
  $mw->messageBox(-message => "I didn't think so either.", -type => "ok");
 }
}
MainLoop;

责编:豆豆技术应用

正在加载评论...