小部件:
框架-用来对其他小部件进行分组。
#!/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;