Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1

160 Chapter 7


This is fine, because the program keeps running even while it’s lis-
tening for mouse clicks, so by the time the user clicks for the first
time, the screen and pen are correctly colored and sized, as shown
in Figure 7-4.

Figure 7-4: A drawing I produced by clicking a few
times with TurtleDrawMax.py

Using the setpos() function as the callback for turtle
.onscreenclick(), we’ve built a fun paint program that interacts
with the user when they click the mouse by drawing lines to
wherever they click. Try customizing the app with different colors,
widths, or anything else you can think of to make it your own.

Listening for Keyboard Events: ArrowDraw


With our turtle drawing program, we saw how listening for mouse
clicks can make the user feel like they’re more in control of the
program. In this section, we’ll learn to use keyboard interaction
to give the user even more options. We’ll also define our own func-
tions to use as event handlers.
In the TurtleDraw.py program, we passed t.setpos as the
callback function to tell the computer what to do when an
onscreenclick() event happened; we wanted to set the turtle’s
Free download pdf