2015.5.16
1. 编写一段程序显示一个标签为“Don't push me”的按钮。当用户点击它时,标签变为“Don't push me again”。
#!/usr/bin/perl
# seems OK
use strict;
use warnings;
use Tk;
my $tk_mw = MainWindow->new();
my $tk_button = $tk_mw->Button(
-text => "Don't push me",
)->pack(
-side => 'top',
-anchor => 'n'
);
$tk_button->configure(
-command => sub {
$tk_button->configure(
-text => "Don't push me agian",
# 再按就直接退出,不添加的话再按不变
-command => \&exit
);
}
);
MainLoop();
2. 编写一个命令,将某条消息作为它的唯一参数,并显示一个包含此消息的弹出窗口。这类命令对于需要显示一条警告或错误信息但没有GUI的脚本有用。
额外了解了一点点 utf8 模块用法。
#!/usr/bin/perl
use strict;
use warnings;
use Tk;
#use encoding "utf-8"; # deprecated
#use encoding "utf8";
use utf8;
my $message = $ARGV[0];
# 总算用对了一次 utf8 模块
utf8::decode($message);
$message = 'No message defined!' unless defined $message;
my $tk_mw = MainWindow->new();
#$tk_mw->geometry('400x100');
$tk_mw->title('Error');
#my $error_frame = $tk_mw->Frame()->pack(-side => 'top');
#$error_frame->Label(
$tk_mw->Label(
-text => $message,
-font => 'Ubuntu 24',
#-background => 'red',
#-foreground => 'white'
-foreground => 'red'
)->pack();
#$error_frame->Button(-text => 'OK', -command => \&exit)->pack(-side => 'bottom');
$tk_mw->Button(-text => 'OK', -command => \&exit)->pack(-side => 'bottom');
MainLoop();