小部件:
框架-用來對其他小部件進行分組。
#!/usr/bin/perl -w use Tk; use strict; my $mw = MainWindow->new; $mw->geometry("200x100"); $mw->title("Frame Test"); $mw->Frame(-background => 'red')->pack(-ipadx => 50, -side => "left", -fill => "y"); $mw->Frame(-background => 'blue')->pack(-ipadx => 50, -side => "right", -fill => "y"); MainLoop;
創建一個使用主窗口作爲父窗口的框架,並將背景顏色設置爲紅色。與前面的 pack 方法示例不同,這裏包括了幾個選項:
$mw->Frame(-background => 'red')->pack(-ipadx => 50, -side => "left", -fill => "y");
第一個參數 ipadx 將框架的寬度增加了 100 (50 x 2)。第二個參數 side 將該框架的幾何位置調整到了其父框架 ($mw) 的左側。第三個參數按照 y 軸方向(垂直)在該框架內分配空間。
2.文本-文本小部件創建一個可編輯的文本工作區
#!/usr/bin/perl -w use Tk; use strict; my $mw = MainWindow->new; $mw->geometry("200x100"); $mw->title("Text Test"); $mw->Text(-background => 'cyan', -foreground => 'white')->pack(-side => "top"); MainLoop;
3. 入口-入口小部件類似於文本小部件,可以在此部件中編輯或輸入文本。文本小部件可讓您輸入多行文本,入口小部件只能輸入一行文本。
#!/usr/bin/perl -w use Tk; use strict; my $mw = MainWindow->new; $mw->geometry("200x100"); $mw->title("Entry Test"); $mw->Entry(-background => 'black', -foreground => 'white')->pack(-side => "top"); MainLoop;
4.按鈕-創建一個可執行函數或命令的按鈕
#!/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;