# https://artofproblemsolving.com/assets/pythonbook/events.html import turtle turtle.setup(500,500) # Determine the window size wn = turtle.Screen() # Get a reference to the window wn.title("Handling Events") # Change the window title wn.bgcolor("lightgreen") # Set the background color tess = turtle.Turtle() # Create our favorite turtle # The next five functions are our "event handlers". def go_straight(): tess.forward(30) def turn_left(): tess.left(45) def turn_right(): tess.right(45) def come_here(x, y): tess.goto(x, y) def quit(): wn.bye() # Close down the turtle window # These lines "wire up" keypresses to the handlers we've defined. wn.onkey(go_straight, " ") wn.onkey(turn_left, "Left") wn.onkey(turn_right, "Right") wn.onkey(quit, "q") # Wire up a click on the window. wn.onclick(come_here) # Now we need to tell the window to start listening for events, # If any of the keys that we're monitoring is pressed, its # handler will be called. wn.listen() wn.mainloop()