## This example draws a green ball. You must click on the canvas to ## start it moving. click again to stop it. ## Copyright 2005 Boley from gdraw import * ## build all the basic objects. circle=fill_oval(20,40,60,80,'green') ## the ball stall() ## draw it now (might not be needed here) CanvasDim=get_width(0) ## where's the right wall? counter=[0] ## count the steps paused = [1] ## global 'paused' state. def Move_Ball (): ## move the ball 1 step. position = get_coords(circle) position[0] += 2 position[2] += 2 if (position[2] >= CanvasDim[0]): ## check for wrap-around position[0]=20 position[2]=60 set_coords(circle,position) counter[0]=counter[0]+1; print "\r ",counter[0]," ", stall() ##(might not be needed here) return position def Move_Loop (): ## Outer Loop to move ball. ## The 'Move_Loop' procedure checks the paused state, moves the ball, ## and finally repeats itself after 50 msec. if paused[0]: print "paused" return Move_Ball() after(50,Move_Loop) def toggle (e): ## toggle pause/nopause state. ## toggle state, then try main task paused[0] = not paused[0] Move_Loop() set_binding(0,toggle) ## what to do upon mouse click. textbox = draw_text("Click here to start and stop",200,300); #CanvasWindow.mainloop() ## Tell Tk to start processing. ## (needed only if -i switch omitted)