Ask coding questions

← Back to all posts
python w/ turtle onkey
nighthowler422 (4)

how to I put onkey for python w/ turtle for pen up and down

Comments
hotnewtop
RYANTADIPARTHI (6018)

Solution

here's an example on how you can use onkey

import turtle

window = turtle.Screen()
window.bgcolor('black')
window.tracer(10)

t = turtle.Turtle()
t.color("red")
t.shape('classic')

def left():
    t.left(90)

def right():
    t.right(90)

def up():
    t.forward(100)

def down():
    t.backward(100)

window.listen()
window.onkey(left, 'Left')
window.onkey(right, 'Right')
window.onkey(up, 'Up')
window.onkey(down, 'Down')

the above code allows you to move up, down, left, or right. The functions make it go in directions, and the onkey allows specific keys to perform the task.

It should work

nighthowler422 (4)

@RYANTADIPARTHI thank you but i am looking for using onkey and making penup and pendown swich whan i press something

RYANTADIPARTHI (6018)

@nighthowler422 what do you mean by penup and pendown switch?

you can just do

t.penup()

# or

t.pendown()

do you mean like that?