先來Po我寫的範例吧(請自行寫出一個呼叫這個副常式的程式):
sub clicked_me(){
my $msgbox=$main->messageBox(
-message=>"Oh? Hi~",
-title=>"Test",
-type=>"yesno",
-icon=>"warning",
-default=>"no"
);
}
我們先宣告一個變數,用來當作是MessageBox的widget
然後,我們把他新增到$main底下(之前就宣告好的主視窗widget)
底下是他的options
-default
這個就是當Message Box跳出來後,按紐焦點在哪個按鈕上
-icon在X11下(也就是*nix or BSD)可以使用Tk內建的bmp圖片,但是在Windows下只能用error、info、question跟warning(也就是錯誤、資訊、問題跟警告)
而且我發現到一件很有趣的事情,error、info、question跟warning這四種在MessageBox跳出來時聲音都不一樣!自己去玩看看吧^^
-message印出來的訊息
-title就是標題拉
-type按鈕的形式,像是 確定、確定 取消、重試 取消、放棄 重試 略過、是 否、是 否 取消
形式:
- OK
- OkCancel
- RetryCancel
- AbortRetryIgnore
- YesNo
- YesNoCancel
hechian 發表在 痞客邦 留言(0) 人氣()
選項通常都是要以'-'開頭,例如:-text
既然都是以'-'開頭,那我就省略不打囉..
- anchor -- 指明資訊如何顯示在widget中。子選項:
- n
- ne
- e
- se
- s
- sw
- w
- nw
- center
- background -- 指明當顯示widget時要使用的正常背景顏色
- bitmap -- widget中要顯示的點矩陣
- boarderwidth -- widget外邊要畫的3D邊界寬度
- cursor -- widget要用的滑鼠游標
- font -- widget內畫文字所要用的字型
- foreground -- widget要用的前景顏色
- geometry -- widget視窗所要的幾何
- highlightbackground -- widget沒有輸入焦點時在遊走突顯區域所要顯示的顏色
- highlightcolor -- widget有輸入焦點時用來遊走繞著widget所畫的突顯矩陣所用的顏色
- image -- widget中的影像
- insertbackground -- 由插入游標所涵蓋背景所要用的顏色
- justify -- 證明顯示widget中的多行文字
- orient -- 定位widget的方向,像是捲動軸
- padx -- 為widget在X方向請求多少空間
- pady -- 為widget在Y方向請求多少空間
- relief -- widget所要的3D效果
- selectbackground -- 當顯示選取的項目時,要用的背景顏色
- text -- 在widget內要顯示的字串
- textvariable -- 變數的名稱
- troughcolor -- 在widget內矩形槽區域所要用的顏色,像是捲動軸與尺度
- underline -- 在widget內要加底線字元的整數索引
- xscrollcommand -- 與水平捲動軸通訊的命令
- yscrollcommand -- 與垂直捲動軸通訊的命令
按鈕選項
- command -- 與按鈕結合的Tcl command
- selectimage -- 當一個check button被選取時要顯示的影像
- height -- 按鈕高度
- selectcolor -- 當一個Button被選取時要使用的背景顏色
- state -- 為一個radio button指定三個狀態之一:
- 正常
- 作用中
- 停止
- variable -- 要設定的全域變數,指出是否一個button被選取
- width -- 按鈕寬度
hechian 發表在 痞客邦 留言(0) 人氣()
- button -- a button
- canvas -- a paint window
- checkbutton -- a check button
- entry -- a input box
- frame -- a simple widget, for a container or spacer, in 複雜的視窗佈局
- image -- a widget for show image
- label -- a label
- listbox -- a list box
- menu -- a menu
- menubutton -- 一個存取選單的選單按鈕
- message -- a message
- radiobutton -- a radio button
- scrollbar -- a scrollbar
- text -- 可編輯的text box
- scale -- a scale
hechian 發表在 痞客邦 留言(0) 人氣()
事件運算:
- bind -- 結合Tcl Script and X events
- bindtags -- 結合commands to label
- selection -- 將widget放在一個frame中的位置
幾何管理員:
- pack -- 將widget包裝,彼此相鄰
- grid -- 將widget包裝,放在欄與列
- place -- 將widget放在一個frame中的位置
視窗運作:
- destroy -- Close a tk window
- toplevel -- Select a top-level window
- wm -- 設定Window特性
- uplevel -- 提升一個視窗層級
hechian 發表在 痞客邦 留言(0) 人氣()
#!/usr/bin/perl -w
use Tk;
use strict;
my $main=MainWindow->new;
my ($label, $text, $button);
$main->title('Test');
$label=$main->Label(
-text=>'Test',
-width=>10
);
$text=$main->Entry(
-width=>10);
$button=$main->Button(
-text=>'Change',
-command=>\&change);
foreach($label, $text, $button){
$_->pack(-side=>"left",-fill=>"both");
}
MainLoop();
sub change(){
$label->configure(-text=>$text->get());
}
hechian 發表在 痞客邦 留言(0) 人氣()
#!/usr/bin/perl
#宣告使用Tk
use Tk;
#使用嚴謹的語法
use strict;
#宣告一個變數供Tk建立視窗
my $main=MainWindow->new;
#設定視窗標題
$main->title('New Window');
#顯示視窗
MainLoop();
hechian 發表在 痞客邦 留言(0) 人氣()
#!/usr/bin/perl -w
use Tk;
use strict;
my $main=MainWindow->new;
my ($n1,$n2,$add,$eq,$result);
$main->title('Test');
$n1=$main->Entry(-width=>3);
$n2=$main->Entry(-width=>3);
$result=$main->Entry(-width=>4);
$add=$main->Label(
-text=>'+');
$eq=$main->Button(
-text=>'=',
-command=>\&do_it);
foreach ($n1, $add, $n2, $eq, $result){
$_->pack(-side=>"left", -fill=>"both");
}
MainLoop();
sub do_it(){
my $sum=$n1->get()+$n2->get();
$result->configure(-text=>$sum);
}
hechian 發表在 痞客邦 留言(0) 人氣()