2011年11月25日 星期五

Laker與TK(更新)


用toplevel建立新窗口.就能讓Tk控制Laker了,看下面例子(source  test.tcl),按Tk按鈕就能讓Layout Zoom in(Zoom out)

 test.tcl


#!/usr/bin/wish
toplevel .main
button .main.a -text "lakerZoomAll" -command { lakerZoomAll }
button .main.b -text "lakerZoomOut" -command { lakerZoomOut }
button .main.c -text "lakerZoomIn" -command { lakerZoomIn }
button .main.f -text "OUIT" -command { destroy .main }
pack .main.b .main.a .main.c .main.f -side top -fill x



窩終於知道了destroy .可以關掉他
新建一個窗口可以用toplevel

新建窗口:

toplevel .main

關掉窗口:

destroy .main


一些TK例子

#!/usr/bin/wish
#建立窗口
toplevel .main
button .main.a -text "lakerZoomAll" -command { lakerZoomAll }
button .main.b -text "lakerZoomOut" -command { lakerZoomOut }
button .main.c -text "lakerZoomIn" -command { lakerZoomIn }
button .main.f -text "OUIT" -command { destroy .main }

radiobutton .main.d -text "YES" -variable var1 -value 3 \
      -command { puts $var1 }
radiobutton .main.e -text "NO" -variable var1 -value 5 \
      -command { puts $var1 }
button .main.g -text "Create main1 frame" -command {
#建立new窗口
toplevel .main1
button .main1.a -text "lakerZoomAll" -command { lakerZoomAll }
button .main1.b -text "lakerZoomOut" -command { lakerZoomOut }
button .main1.c -text "lakerZoomIn" -command { lakerZoomIn }
button .main1.f -text "OUIT" -command { destroy .main1 }
pack .main1.b .main1.a .main1.c .main1.f -side top -fill x
}
button .main.h -text "Delete main frame" -command {
#刪除窗口
destroy .main
}

#輸入框變數
set val {Well come!}
#輸入框
entry .main.text -textvariable val
#取出輸入框值
button .main.i -text "PUTS" -command { puts $val }
#循環框
spinbox .main.cnt -from 1 -to 20 -textvariable var -increment 1 -wrap yes
#選項菜單 val和entry一樣,會顯示相同
tk_optionMenu .main.option val lakerZoomAll lakerZoomOut lakerZoomIn

pack .main.b .main.a .main.c .main.f .main.text .main.i .main.cnt .main.option .main.d .main.e .main.g .main.h -side top -fill x



 一個完整例子



#!/usr/bin/wish
#wm deiconify .
#wm title . "test"
#新建窗口
toplevel .main
wm title .main "TOP LEVEL"
#指定窗口大小和位置
wm geometry .main 400x140+100+100; update
wm maxsize .main 1028 512
wm minsize .main 128 1


#輸入框1
label .main.l_data -text "INPUT BOUNDARY LAYER:"
place .main.l_data -x 10 -y 10
entry .main.addr -textvariable addr
place .main.addr -x 180 -y 10
#輸入框2
label .main.l1_data -text "INPUT TEXT LAYER:"
place .main.l1_data -x 10 -y 40
entry .main.addr1 -textvariable addr1
place .main.addr1 -x 180 -y 40
#按鈕OK
button .main.a -text "OK" -command { lakerMessage "BOUNDARY LAYER : $addr TEXT LAYER : $addr1"  }
place .main.a -x 20 -y 80
#按鈕Close
button .main.a1 -text "Close" -command { destroy .main  }
place .main.a1 -x 100 -y 80

#pack .main.l_data .t.addr .t.a -side top

tk較複雜例子(根據使用者輸入值在canvas畫布畫圓)

#!/usr/bin/wish
#wm deiconify .
#wm title . "test"
#新建窗口
toplevel .main
wm title .main "Input String Field"
#指定窗口大小(400x400)和位置(60,60)
wm geometry .main 400x400+60+60; update
wm maxsize .main 1028 512
wm minsize .main 128 1
set cx 100
set cy 100
set cr 50
#在窗口開一個frame
frame .main.frame1 -bd 2 -width 400 -height 300 -relief raised
place .main.frame1 -x 0 -y 0
#在frame建立畫布
canvas .main.frame1.canvas
place .main.frame1.canvas -x 0 -y 0
#在frame建立輸入框x
label .main.frame1.lx -text "X:"
place .main.frame1.lx -x 2 -y 270
entry .main.frame1.ex -width 5 -textvariable cx
place .main.frame1.ex -x 20 -y 270
#在frame建立輸入框y
label .main.frame1.ly -text "Y:"
place .main.frame1.ly -x 82 -y 270
entry .main.frame1.ey -width 5 -textvariable cy
place .main.frame1.ey -x 100 -y 270
#在frame建立輸入框r
label .main.frame1.lr -text "R:"
place .main.frame1.lr -x 162 -y 270
entry .main.frame1.er -width 5 -textvariable cr
place .main.frame1.er -x 180 -y 270
tk_optionMenu .main.frame1.to color red blue yellow green
place .main.frame1.to -x 260 -y 270
tk_optionMenu .main.frame1.tol line_width 1 2 3 4 5
place .main.frame1.tol -x 340 -y 270
按鈕Draw
button .main.a -text "DRAW" -command { draw_circle  $cx $cy $cr $line_width $color }
place .main.a -x 20 -y 360
#按鈕Close
button .main.a1 -text "Close" -command { destroy .main  }
place .main.a1 -x 100 -y 360
proc draw_circle {x y r line_width color} {
set x0 [ expr $x - $r ]
set y0 [ expr $y - $r ]
set x1 [ expr $x + $r ]
set y1 [ expr $y + $r ]
.main.frame1.canvas create oval $x0 $y0 $x1 $y1 -fill $color -width $line_width;update
}

沒有留言: