Here's a complete example -- note that you were also not showing the listen()
method in your code fragment which is also required:
from turtle import Screen, Turtle
def handler():
turtle.write("Button pressed!", align='center', font=('Arial', 18, 'normal'))
screen = Screen()
turtle = Turtle()
turtle.hideturtle()
screen.onkeypress(handler)
screen.listen()
screen.exitonclick()
Here's the story: The onkey()
method is also known as onkeyrelease()
and neither accepts None
as a character, nor a missing character argument. @TimRoberts comment won't work. As @rdas notes (+1), use the onkeypress()
method which does accept a None
character, or simply a missing argument, and does what you want.
But here's a catch: Your event handler will trigger on any key, but you've no means within Turtle to determine which key. If you need that functionality, look at this answer which provides a replacement onkeypress()
method that passes the character typed to your event handler, in the case where a character was not specified. (I.e. the None
case.)