Perl/Tk练习

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();

转载于:https://my.oschina.net/u/727594/blog/415606

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值