import turtle

turtle.setup(400,500)                # Determine the window size
wn = turtle.Screen()                 # Get a reference to the window
wn.title("Handling keypresses!")     # Change the window title
wn.bgcolor("lightgreen")             # Set the background color
tess = turtle.Turtle()               # Create our favorite turtle

# The next four functions are our "event handlers".
def h1():
   tess.forward(30)

def h2():
   tess.left(45)

def h3():
   tess.right(45)

def h4():
    wn.bye()                        # Close down the turtle window
def red():
    tess.color("red")
def blue():
    tess.color("blue")
def green():
    tess.color ("green")
def plus():
    size = tess.pensize()
    if size <= 20 and size >0:
        tess.pensize(size +1)

def minus():
    size = tess.pensize()
    if size <= 20 and size >0:
        tess.pensize(size -1)

# These lines "wire up" keypresses to the handlers we've defined.
wn.onkey(h1, "Up")
wn.onkey(h2, "Left")
wn.onkey(h3, "Right")
wn.onkey(h4, "q")
wn.onkey(red, "r")
wn.onkey(blue, "b")
wn.onkey(green, "g")
wn.onkey(plus, "+")
wn.onkey(minus, "-")

# 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()