Functions: There’s a Name for That 163
Figure 7-5: The ArrowDraw.py program lets the user draw
using the up, right, and left arrow keys. The larger turtle
arrow makes it easier to see where the turtle is headed.
This app is a bit like the old Etch-A-Sketch toy: you can draw
fun shapes using just those three arrow keys, and you can retrace
your steps. Feel free to customize the app with your own colors,
pen width, and any other features you’d like to add. One extra fea-
ture you could add, which is included as a challenge at the end of
this chapter, is the ability to click to move the turtle to a new loca-
tion. Dream up new features and give them a try—that’s the best
way to learn something new!
Handling Events with Parameters: ClickSpiral
In TurtleDraw.py, we let the user click to draw by telling the
turtle.onscreenclick() listener to call the t.setpos function anytime
the user clicked the screen. Let’s build a new program named
ClickSpiral.py that will draw spirals wherever the user clicks, as
shown in Figure 7-6.
The onscreenclick() listener passes the x- and y-coordinates
of every mouse click as arguments to the callback function we
specify. When we want to handle mouse click events with a func-
tion of our own, we simply write a function that accepts those
values—the x- and y-coordinates of the mouse click—as a pair of
parameters.