TCL-TK介面
本文簡單介紹tcl-tk,並附一個介面案例。最後上傳了三個附件,關於tcl-tk編寫的手冊。
tck-tk手冊在如下網頁點選下載:http://pan.baidu.com/s/1jHXGXpW
首先,Tcl是一種簡單的指令碼語言,主要使用於釋出命令給一 些互交程式如文字編輯器、偵錯程式和shell。它有一個簡單的語法 和很強可擴充性,Tcl可以建立新的過程以增強其內建命令的能力。
其次,Tcl是一個庫包,可以被嵌入應用程式,Tcl的庫包含了一個分析器、用於執行內建命令的例程和可以使你擴充(定義新的 過程)的庫函式。應用程式可以產生Tcl命令並執行,命令可以由使用者產生,也可以從使用者介面的一個輸入中讀取(按鈕或選單等)。但Tcl庫收到命令後將它分解並執行內建的命令,經常會產生遞迴的呼叫。
TK 是與 TCL 語言關係最密切、且被廣泛使用的圖形庫,TCL 語言和 TK 都由 JohnOusterhout 開發。雖然 TK 於 1991 年作為 X11 庫出現,但實際上它從那時起就被移植到每一種流行的 GUI。(它與 Python 逐漸擁有“標準”GUI 的情形相似。)現在,大多數流行語言和很多小型語言都有 TK 繫結(Tkinter 模組)。
本例項程式碼對應的介面截圖
程式碼如下:
#wm title . "NS2環境中OLSR協議的研究及模擬"
grid [frame .f] -column 0 -row 0 -sticky nwes
grid columnconfigure . 0 -weight 1
grid rowconfigure . 0 -weight 1
#命名模擬檔案
grid [label .f.laType -text "模擬檔案命名" ] -column 1 -row 1 -sticky nwes
grid [entry .f.fileName -width 7 -textvariablefileName ] -column 2 -row 1 -sticky nwes
#選擇拓撲型別
grid [label .f.latopotype -text "拓撲型別"]-column 1 -row 2 -sticky nwes
grid [radiobutton .f.line -text Line -variabletopotype -value Line] -column 2 -row 2 -sticky w
grid [radiobutton .f.matrix -text Matrix-variable topotype -value Matrix] -column 3 -row 2 -sticky w
grid [radiobutton .f.random -text Random-variable topotype -value Random] -column 4 -row 2 -sticky w
#選擇協議
grid [label .f.laRout -text "協 議"] -column 1-row 3 -sticky nwes
grid [radiobutton .f.olsr -text OLSR -variableroupro -value OLSR] -column 2 -row 3 -sticky w
grid [radiobutton .f.aodv -text AODV -variableroupro -value AODV] -column 3 -row 3 -sticky w
grid [radiobutton .f.dsr -text DSR -variableroupro -value DSR] -column 4 -row 3 -sticky w
#輸入節點數量
grid [label .f.laNodeNum -text "節點數量"]-column 1 -row 4 -sticky nwes
grid [entry .f.nodeNum -width 7 -textvariablenodeNum ] -column 2 -row 4 -sticky w
#輸入產生流量節點數量
grid [label .f.laNodeNum2 -text "產生流量節點數量"] -column 3 -row 4 -sticky nwes
grid [entry .f.lanodeNum -width 7-textvariable lanodeNum ] -column 4 -row 4 -sticky w
#生成節點總數
grid [label .f.laNodeNum3 -text "傳送資料包總數"] -column 1 -row 5 -sticky nwes
grid [entry .f.allnode -width 7 -textvariableallnode ] -column 2 -row 5 -sticky w
#設定網路介面佇列大小
grid [label .f.laqlen -text "介面佇列大小"] -column 3 -row 5 -sticky nwes
grid [entry .f.qlen -width 7 -textvariableqlen ] -column 4 -row 5 -sticky w
#選擇流量型別
grid [label .f.laTcpUdp -text "Cbr流量型別"]-column 1 -row 6 -sticky nwes
#grid [radiobutton .f.cbr -text Cbr -variableflowType -value cbr] -column 2 -row 6 -sticky w
grid [radiobutton .f.tcp -text Tcp -variableflowType -value tcp] -column 2 -row 6 -sticky w
grid [radiobutton .f.udp -text Udp -variableflowType -value udp] -column 3 -row 6 -sticky w
# 設定傳送包大小
grid [label .f.lapacketsize -text"packetsize"] -column 1 -row 7 -sticky nwes
grid [entry .f.packetsize -width 7-textvariable packetsize ] -column 2 -row 7 -sticky w
grid [label .f.danwei -text "位元組"]-column 3 -row 7 -sticky w
#是否啟用nam
grid [label .f.lanam -text "執行nam"]-column 1 -row 8 -sticky nwes
grid [radiobutton .f.isnam -text isnam-variable isnam -value isnam] -column 2 -row 8 -sticky nwes
grid [radiobutton .f.nonam -text nonam-variable isnam -value nonam] -column 3 -row 8 -sticky nwes
#選擇連線的顏色
grid [label .f.laColor -text "連結顏色"]-column 1 -row 9 -sticky nwes
grid [radiobutton .f.red -text red -variablecolor -value red] -column 2 -row 9 -sticky w
grid [radiobutton .f.blue -text blue -variablecolor -value blue] -column 3 -row 9 -sticky w
grid [radiobutton .f.yellow -text yellow-variable color -value yellow] -column 4 -row 9 -sticky w
#檢視結果按鈕
grid [button .f.save -text "儲存設定"-command prWrite] -column 1 -row 10 -sticky nwes
grid [button .f.run -text "執行程式"-command prRun] -column 2 -row 10 -sticky nwes
grid [button .f.throughPutR -text "吞吐量檢視"-command prThroughPutR] -column 3 -row 10 -sticky nwes
#grid [button .f.throughPutP -text "吞吐量繪圖"-command prThroughPutP] -column 4 -row 7 -sticky nwes
#grid [button .f.delay -text "時延"-command prDelay] -column 3 -row 7 -sticky nwes
#
#隨機生成兩個節點產生流量
#
set ::nodeNum 6
set ::lanodeNum 3
set ::allnode 10000
set ::qlen 500
set ::packetsize 1000
set ::topotype Line
set ::roupro OLSR
set ::color red
proc prWrite {} {
#將圖形介面中選擇的資料寫入到一箇中間檔案中
#在tcl主程式中讀取這個檔案中的資料,進行設定
setf [open ./myvalues w]
puts $f $::fileName
puts $f $::topotype
puts $f $::roupro
puts $f $::nodeNum
puts $f $::lanodeNum
puts $f $::allnode
puts $f $::qlen
puts $f $::flowType
puts $f $::packetsize
puts $f $::color
close $f
}
#資料分析
proc prRun {} {
execsh runall.sh & exit 0
#}
proc prThroughPutR {} {
execsh readresult.sh & exit 0
}
#proc prThroughPutP {} {
# execgnuplot -p -e "set xlabel '時間(s)';set ylabel '吞吐量(kb)'; plot 'throughput_OLSR' with lines,'throughput_AODV' with#lines,'throughput_DSR' with lines"
#}
#proc prDelay {} {
# execgnuplot -p -e "set xlabel '時間(s)';set ylabel '延遲(s)'; plot 'delay_OLSR' with lines,'delay_AODV' withlines,'delay_DSR' with #lines" gawk–f throughput.awk olsr_wireless.tr > throughput_***
#}