(original) (raw)
# click_and_stamp """test program to explore the relationship between turtle clicks, as enabled by turtle.onclick(), and the turtle.stamp() function. """ from turtle import TurtleScreen, RawTurtle, TK tom = None # this will be the turtle turtle_stamp = None # this will be an IntVar for the 'stamp' checkbox # initializing function to activate screen clicks def init_screen_clicks(scrn): scrn.onclick(left_click, 1) # 1 indicates left mouse button scrn.listen() # send the turtle to the location where the mouse was clicked def send_turtle(t, x, y): pos = t.towards(x, y) t.setheading(pos) t.goto(x, y) print('turtle moved to x', x, 'y', y) # this is the event handler for the left click def left_click(x, y): global tom send_turtle(tom, x, y) if turtle_stamp.get(): print('executing turtle.stamp()') tom.stamp() # this is the event handler for turtle clicks # we should always get a left_click immediately after the turtle click def click_turtle(x, y): global tom print('************* turtle click detected *************') tom.left(720) # make it obvious that something happened # turtle setup def init_turtle(t): t.shape('turtle') t.turtlesize(1) t.speed(6) t.onclick(click_turtle, 1) # this is the event handler for the undo() button def turtle_undo(): global tom print('executing turtle.undo()') tom.undo() # this is the event handler for the fd(0) button def turtle_fd(): global tom print('executing turtle.fd(0)') tom.fd(0) # this is the event handler for the left(0) button def turtle_left(): global tom print('executing turtle.left(0)') tom.left(0) # here is where we add the undo button and the stamp checkbox def add_widgets(root): global turtle_stamp btn_width = 6 btn_height = 2 turtle_stamp = TK.IntVar() turtle_stamp.set(False) btn_undo = TK.Button(root, text='undo()', width=btn_width, \ height=btn_height, command=turtle_undo) btn_undo.pack(side='left', padx=10, pady=2) btn_fd = TK.Button(root, text='fd(0)', width=btn_width, \ height=btn_height, command=turtle_fd) btn_fd.pack(side='left', padx=10, pady=2) btn_left = TK.Button(root, text='left(0)', width=btn_width, \ height=btn_height, command=turtle_left) btn_left.pack(side='left', padx=10, pady=2) chkbox = TK. Checkbutton(root, text='Stamp', variable=turtle_stamp) chkbox.pack(side='right', padx=10, pady=2) # create the canvas def my_canvas(root, cv_width, cv_height, cv_bg): cv = TK.Canvas(root, width=cv_width, height=cv_height, bg=cv_bg) cv.pack() add_widgets(root) return cv # the following constants determine the dimensions of the game window scn = None cv_left = -200 cv_right = 200 cv_top = 150 cv_bottom = -150 cv_width = abs(cv_left) + cv_right cv_height = abs(cv_bottom) + cv_top def main(): global tom root = TK.Tk() root.title('Click and Stamp') cv = my_canvas(root, cv_width, cv_height, '#ddffff') scn = TurtleScreen(cv) scn.bgcolor(0.85, 0.85, 1) tom = RawTurtle(scn) init_turtle(tom) init_screen_clicks(scn) return 'EVENTLOOP' if __name__ == '__main__': main() TK.mainloop() # keep window open until user closes it