2011年12月3日 星期六

TK的canvas元件,清除畫面

 畫了3個圓
按清除,被清空畫布

 使用set將圓形放入變數中
 set cir [$obj create oval $x0 $y0 $x1 $y1 -fill $color -width $line_width ]
 就可使用delete來清除了
 $obj delete $dc


範例:


#!/usr/bin/wish
#wm deiconify .
#wm title . "test"
#新建窗口
global circle
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 ::c1 {}
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 "畫圓" -command { draw_circle .main.frame1.canvas  $cx $cy $cr $line_width $color }
place .main.a -x 20 -y 360
#按鈕Close
button .main.a1 -text "清除" -command {
delete_circle .main.frame1.canvas $::circle 

unset ::circle
#destroy .main 
}
place .main.a1 -x 100 -y 360
#按鈕Close
button .main.a2 -text "關閉" -command {
destroy .main
}

place .main.a2 -x 200 -y 360
proc draw_circle {obj x y r line_width color } {
global node
set x0 [ expr $x - $r ]
set y0 [ expr $y - $r ]
set x1 [ expr $x + $r ]
set y1 [ expr $y + $r ]
set cir [$obj create oval $x0 $y0 $x1 $y1 -fill $color -width $line_width ]
lappend ::circle $cir
puts $::circle
}
#將畫布上組件list清除
proc delete_circle {obj list_circle} {
foreach dc $list_circle {
$obj delete $dc
}
}

沒有留言: