Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key event seems stuck using `turtle.onkey(function(), "key")`

I'm trying to add keyboard input to move python's turtles but without even pressing the assigned key the turtle moves as if I'm holding the assigned key.

What am I doing wrong?

My code is below:

# import
import turtle

# init screen, turtle
window = turtle.Screen()
turt = turtle.Turtle()
turt.speed(5)

def up():
    turt.forward(10)
def left():
    turt.left(10)
def right():
    turt.right(10)

while True==True:
    turtle.onkey(up(), "Up")
    turtle.onkey(left(), "Left")
    #turtle.onkey(right(), "Right")

# window await
turtle.listen()
window.mainloop()

1 Answers

Rather than calling screen.onkey(function(), "key") you call screen.onkey(funtion, "key")

So

turtle.onkey(up(), "Up")

becomes

turtle.onkey(up, "Up")

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!