Tcl/Tk 烹调书 - 使用画布


Part-I - 第 3 步: 为动画建立一个对象并通过按钮来控制动画

向你的包含有步骤 1 和 2 脚本的文件添加这个脚本。过程 displayInfo 调用在上一步中描述的 "movie":



proc displayInfo {} {
    .mv.c create text 10 10 -text "A Moving Story" -tags st
    .mv.c create text 20 30 -text "by" -tags st
    .mv.c create text 15 50 -text "Venkat V V S S Sastry" -tags st
     bell
     movie st 250 
    .mv.c.but configure -text Stop -command \
		{global id
		 bell
    		 after cancel $id
       		.mv.c.but configure -text {The End}
    		 after 10000
    		.mv.c.but configure -text Quit -command {exit} 
    		}
}

在"displayInfo"中的前面三行在画布中由(10,10)、(20,30)和(15,50)给出的 x,y 位置上建立三个独立的文本对象。这三个字符串都被赋予一个单一的标签名"st"。

命令 bell 鸣放应用的主窗口的显示器的震铃。你可以给 bell 一个 -displayof 选项来指定一个特定的窗口的显示器。接着 "displayInfo" 调用过程 "movie",参数是标签名 "st" 和一个间隔(这里是 250 微秒)。在按钮上的标签变为 "Stop"。

按钮的 -command 选项被重新配置来取消用标识符"id"给出的延迟命令的执行。如果用户点击这个按钮,标签又被变成"The End"来标记动画的结束。在流逝 10 秒钟之后按钮显示"Quit"来让用户点击它退出应用。