Perl/Tk練習

2015.5.16less

1. 編寫一段程序顯示一個標籤爲「Don't push me」的按鈕。當用戶點擊它時,標籤變爲「Don't push me again」。ide

#!/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的腳本有用。oop

額外瞭解了一點點 utf8 模塊用法。code

#!/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();
相關文章
相關標籤/搜索