In this article, we will show you how to use the Turtle library to make shapes in Python. Furthermore, we will also show you how to control these shapes using Event Handling.
The first thing to do is to import the relevant library (Well, the first thing to do is to open a new IDLE Editor window, but let's assume we did that. If you have not, go ahead and open one). In the editor, type:
import turtle
Unlike other imports, however, we need to type turtle.FunctionName to use any of its functions and classes. We can avoid this by importing them separately:
from turtle import FunctionName1
from turtle import FunctionName2
Or we can import them all:
from turtle import *
But for now, we are going to stick with referencing the library in-line.
We first need to make a screen object. This will allow us to draw things on it. To make a screen object, we use the below code:
wn=turtle.Screen()
The variable wn is used so that we can refer to screen if we need to. We can use any other name, though.
We now need to make the object that we will use to make shapes. For that, we type:
t=turtle.Turtle()
This way, we make out turtle object t.
So far, our code should look like this:
Before we run it, though, we need to add a very crucual piece of code: wn.mainloop(). This will allow us to constantly refresh the screen object, hence we can actually see the output.
If we run it, we get:
Aside from the shell window, We got a window named Python Turtle Graphics. This is is where the actual output of our program will be seen.
The cursor in the center is called a turtle, and it is basically like a pen. You can move it around using the code functions t.forward(Dist) and t.backward(Dist). Here, Dist is the number of pixels the turtle is supposed to move.
We can also turn it using the t.left(Deg) and t.right(Deg) functions. Here, Deg is the amount the turtle is supposed to rotate in degrees.
Try experimenting with these functions and make a drawing. Here is the program for a simple square:
There are some other functions as well. Here are the most prominent ones:
t.penup()
"Lifts" the "pen," so the turtle can be moved without drawing
t.pendown()
"Lowers" the "pen."
t.color("colorName")
Changes the color of the pen. Standard names (such as red, blue, yellow) and RGB values work.
t.speed(number)
Changes the speed the turtle moves and, hence, changes the speed at which it draws.
Now lets get into the meat of this topic: Event handling.
Event handling basically checks what keypresses or mouse interactions happened, and can do something accordingly.
In this case, we can use the arrow keys on the keyboard to control the turtle.
To make this, we first define some functions. In each function, we put the appropriate controls to move or rotate the turtle.
Then we program the keypresses. This is where the screen object comes in. The function wn.onkey(FunctionName, "keypress"), in this case, will allow us to register a keypress with a function. For arrow keys, we write "Up", "Down", "Left", and "Right" in the function.
There is another important line of code: wn.listen(). This will tell the program to listen for keyboard and mouse inputs. Without this, it will not do anything.
With that, here is the final program:
Try experimenting with these functions. See what works and what does not.
With that, you just made some shapes and lines in Python!