# this function gets called when the user presses a key. # the following try statement is a hack; don't emulate this. try: # figure out which function to call, and call it func = eval('draw_' + event.char) except NameError: print "I don't know how to draw an", event.char return except SyntaxError: # this happens when the user presses return teleport(bob, -180, bob.y - size * 3) return func(bob, size) skip(bob, size / 2) from TurtleWorld import * world = TurtleWorld() # create and position the turtle size = 20 bob = Turtle(world) bob.delay = 0.01 teleport(bob, -180, 150) # tell world to call keypress when the user presses a key world.bind('<Key>', keypress) world.mainloop()
def keypress(event): # this function gets called when the user presses a key. # the following try statement is a hack; don't emulate this. try: # figure out which function to call, and call it func = eval('draw_' + event.char) except NameError: print "I don't know how to draw an", event.char return except SyntaxError: # this happens when the user presses return teleport(bob, -180, bob.y-size*3) return func(bob, size) skip(bob, size/2) from TurtleWorld import * world = TurtleWorld() # create and position the turtle size = 20 bob = Turtle(world) bob.delay = 0.01 teleport(bob, -180, 150) # tell world to call keypress when the user presses a key world.bind('<Key>', keypress) world.mainloop()