The Python Turtle Library is a really cool Python library that gives users the ability to draw various shapes by providing them with a virtual canvas. The drawing tool that makes all of the shapes, lines, and colors appear on the canvas is called the turtle, and that is why the program is called Turtle! We can try out the Python Turtle module but first, it needs to be imported. In this tutorial, we’ll set up a Python file and get ourselves ready to use the Turtle library.
Start with a blank Python file. It can have any name, we can just use testing.py to get started. Inside of testing.py, add the following line of code.
from turtle import *
The from command just means to import something from outside the current file. After that comes the name of the module we want to import from, which is turtle. Using the import keyword allows us access to all of the code from the turtle module. The asterisk (*) at the end of the line is a wildcard that tells Python to import everything from that module.
Setting A Screen Size
Turtle will set up a window automatically for you when the program is launched. If you want to set a custom window size for use with your program, you can do so using Screen() and the .setup() method. The code here is going to give us a Turtle window that has a width of 750 and a height of 500.
drawing_area = Screen() drawing_area.setup(width=750, height=500)
My Turtle Window Disappeared!
At this point, the entire code of our tiny little Turtle program looks like this.
from turtle import * drawing_area = Screen() drawing_area.setup(width=750, height=500)
When we run the program, however, we see a Turtle window flash on the screen for a split second and then disappear. That’s not too good! To fix that, we can use the done() method.
from turtle import * drawing_area = Screen() drawing_area.setup(width=750, height=500) done()
Now when you run the program, you should see a beautiful blank canvas just waiting for your creativity to be unleashed!
Next up, we’ll see how to move the Turtle in Python.