13.3 Event-Driven Programming with the Turtle

The turtle module provides three methods to register callbacks for key, mouse, and timer events:

  •    onkey

  •    onclick

  •    ontimer

In addition, the listen method is used to give the turtle canvas focus so that the event handler can respond to key presses. Each method has conventions for the parameters it accepts. TABLE 13.3 explains each of these methods to register callbacks for events, as well as the listen method.

TABLE 13.3 Turtle Methods to Register Callbacks

Method

Description

onkey(function, key)

Bind a function to a key press so that function is called when a specified key is pressed. function must be a function with no parameters. key is a string representing a key on the ...

Get Python Programming in Context, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.