Perl/TKx 常用窗口控件

1. 创建提示框:

Tkx::tk___messageBox(-type => "yesno", 
    -message => "Are you sure you want to install SuperVirus?",
    -icon => "question", 
    -title => "Install"
); 

tk___messageBox的各个参数含义如下:

type:提示框类型,显式的按钮有所不同。

ok (default) "ok"

okcancel "ok" or "cancel"

yesno "yes" or "no"

yesnocancel "yes", "no" or "cancel"

retrycancel "retry" or "cancel"

abortretryignore "abort", "retry" or "ignore"

message: 提示框上显示的内容

detail: 用小一号字体显示的详细内容

title: 对话框的标题,Mac OS X不可用.

icon: 对话框的图标:"info" (default), "error", "question"和"warning".

default: 指定默认focus的按钮

parent: 提示框窗口的父窗口,居中显示在父窗口上

2. 文件对话框很简单直接:

$filename = Tkx::tk___getOpenFile();
$filename = Tkx::tk___getSaveFile();
$dirname = Tkx::tk___chooseDirectory();

3. 窗口居中:从OverStack上Copy下来的函数,个人验证还是存在一些问题,并不是完全居中。 

sub CenterWindow {
    # Args: (0) window to center
    # (1) [optional] desired width
    # (2) [optional] desired height

    my ($window, $width, $height) = @_;

    Tkx::update('idletasks');
    $width  ||= Tkx::winfo('reqwidth',  $window);
    $height ||= Tkx::winfo('reqheight', $window);

    my $x = int((Tkx::winfo('screenwidth',  $window) / 2) - ($width / 2));
    my $y = int((Tkx::winfo('screenheight', $window) / 2) - ($height / 2));

    $window->g_wm_geometry($width . "x" . $height . "+" . $x . "+" . $y);
}

4. Frame

my $mw = Tkx::widget->new(".");
my $content = $mw->new_ttk__frame(-padding => "3 3 12 12");

5. 标签

my $hi = $content->new_ttk__label(-text => "Hi!");

6. 复选框,注意关联的变量是不同的

my $one = $content->new_ttk__checkbutton(-text => "One", -variable => \$option_one, -onvalue => 1);
my $two = $content->new_ttk__checkbutton(-text => "Two", -variable => \$option_two, -onvalue => 1);
my $three = $content->new_ttk__checkbutton(-text => "Three", -variable => \$option_three, -onvalue => 1);

7. 单选框,关联变量是同一个

my $display1 = $content->new_ttk__radiobutton(-text => "Display 1", -variable => \$display, -value => "DISPLAY1");
my $display2 = $content->new_ttk__radiobutton(-text => "Display 2", -variable => \$display, -value => "DISPLAY2");

控件的初始值又关联变量的初始值决定。

8. 下拉列表框

my $combo = $content->new_ttk__combobox(-textvariable => \$city, -state => "readonly");
$combo->configure(-values => "Chengdu Shanghai");

-state参数表明下拉列表框是不可编辑的,只能选择。列表的值通过configure -values设置,空格作为每个选择项的分隔符。

9. 按钮

my $ok = $content->new_ttk__button(-text => "Okay", -command => sub {
    #...
});

10. 布局,每个控件都可以调用g_grid来通过网格进行布局。

$combo->g_grid(-column => 0, -row => 5, -columnspan => 3, -sticky => "ew", -pady => 10);
$content->g_grid_columnconfigure(1, -weight => 3);
$content->g_grid_rowconfigure(1, -weight => 1);
$content->g_pack;

你可能感兴趣的:(Perl/TKx 常用窗口控件)