Tcl/Tk 烹调书 - 重访画布


第 2 步: 添加其他 GUI 对象

脚本


button .rect  -image [image create bitmap \
-file  "./bitmaps/boxOp.xbm"]  -com {set sb "rectangle"}
button .circ  -image [image create bitmap -file  "./bitmaps/ovalOp.xbm"] \
	 -com {set sb "oval" }
button .lin  -image [image create bitmap -file  "./bitmaps/lineOp.xbm"] \
	 -com {set sb "line" }
button .txt  -image [image create bitmap -file  "./bitmaps/textOp.xbm"] \
	 -com {set sb "text"}
button .selob  -image [image create bitmap -file  "./bitmaps/selectOp.xbm"] \
	 -com {set sb "obj" }
label .txtlab -text "Text : "
entry .txtstr -textvariable str -relief sunken -width 10

place .rect -in .fr.panl -x 2 -y 1
place .circ -in .fr.panl -x 60 -y 1
place .lin -in .fr.panl -x 2 -y 60
place .txt -in .fr.panl -x 2 -y 120
place .selob -in .fr.panl -x 60 -y 60
place .txtlab -in .fr.panl -x 2 -y 180
place .txtstr -in .fr.panl -x 20 -y 220

图象

使用 Tk 命令 image 来建立、删除或查询图象。给 image 的第一个参数指定要做的动作(例如,create )而第二个参数是图象的类型(例如,bitmap)。"-file" 选项从指定的文件中读取位图数据。

"-image" 选项替换了在按钮建立命令中的 "-text" 选项,来给按钮放置位图图象作为标签。每个按钮命令把全局变量 "sb" 设置成一个指定的值。在画布绘图例程中使用这些值来确定要画制那种类型的绘图基本操作。例如,用户选择了 "rectangle" 按钮;"sb" 被赋值为 "rectangle" 并且在画布中命令 .can create $sb $x $y $x1 $y1 画一个矩形对象(物体)。

文本

在这个例子中,在画布中绘制文本被简化了。用户在录入组件中输入文本串,击 return 键并选择标记为"A"的按钮。接着在画布中点击鼠标左键的地方建立文本串。

选择一个对象

使用有"虚线的矩形"标签的按钮来在画布组件中指定一个矩形区域。画出红色轮廓来标记这个矩形。

放置组件的结果是

只在用户显式另一个基本操作的按钮的时候改变当前的绘图基本操作。