Programming with Python Turtle graphics
Turtles
Create a bulb and tube analog thermometer with a Raspberry Pi and Python Turtle graphics.
Turtle graphics are a great way to get kids started programming. The Python turtle library offers simple step-by-step graphical methods that allow young programmers to create graphics with only a few lines of code.
I found that once my kids understood Turtle basics, we were able to do more advanced Raspberry Pi projects that used sensors, push buttons, and outputs. In this article, I share a Rasp Pi Turtle project that creates a graphic of an "old style" mercury thermometer animated with a Pi temperature sensor.
Getting Started
The Python turtle library is probably already loaded on your Raspberry Pi system, but if it is not, enter:
pip install turtles
The turtle library [1] is used a little bit as you would use a pen on a drawing board. With the use of methods (e.g., forward()
, backward()
, left()
, and right()
), you can draw lines. If you want to move a turtle without drawing a line, you use the penup()
method; then, once you arrive at the desired location, the pendown()
method lets you start drawing again. Turtle objects have properties for color and line thickness and a number of additional methods for shapes, fill, circles, and text.
To create a turtle object, you use the myturtle = Turtle()
statement. A new turtle is placed at the center of the screen, position (0,0), facing right.
The Python Turtle example in Listing 1 shows three basic concepts I use in this project: drawing lines, moving without drawing, and adding text (Figure 1).
Listing 1
turtle1.py
To draw t1
, named turtle
(line 7), the turtle (pen) rotates 90 degrees left to face toward the top of the drawing area (line 9). With a combination of left
and forward
methods, turtle t1
becomes an open rectangle (lines 9-14).
The t2
shape is red and drawn with a thick pen size (lines 18-20). The up()
method makes sure the pen does not draw while it moves (line 21-22). After reaching the new position, the pen goes back down to draw a circle (lines 23-24). The pen for turtle t3
goes to a new position without drawing a line and writes some text on the screen (line 27-31).
Once you (or your young programmer) have mastered drawing lines, moving without drawing, and adding text, you can start some more interesting projects.
Drawing a Thermometer
For this project, I wanted to draw an "old style" mercury thermometer with a bulb of red mercury at the bottom and a tube above it (Figure 2).
Simple turtle commands like move
, left
, right
, and forward
are great for drawing simple graphics, but they can be awkward if you want to create a more complex drawing. A more efficient approach is to define an array of (x, y) coordinates and move to each position in the array. For example, the upper tube can be drawn with:
# An array of coordinates for the tube outline = ((25,-50),(25,210),(-25,210),(-25,-50)) for pos in outline: # move to each tube x,y point t1.goto(pos)
To draw the lower "mercury" bulb in this project, I enhance the circle of my first example, turtle t2
, by creating a black circle with red fill. The fill is toggled with the begin_fill()
/end_fill()
methods:
# put the pen up and move to the circle starting point t2.penup() t2.goto(0,-137) t2.pendown() t2.pensize(5) t2.color("black","red") # draw the circle with fill t2.begin_fill() t2.circle(50) t2.end_fill()
At this point, I've created a static background for the thermometer. The next step is to read a temperature value from the Raspberry Pi and add dynamic information to the drawing.
Hardware Setup
A number of different temperature sensors can be used for this project. Here, I use a low-cost ($5) DHT11 temperature/humidity sensor [2]. This sensor has three pins, (signal, 5V, and GND; Figure 3). The signal pin connects to Rasp Pi physical pin 7 (PP7). The program in Listing 2 tests the DHT11 sensor. Note that because the sensor can return a humidity value, you could add that information in your Turtle code, as well (line 12).
Listing 2
test.py
To install the DHT temperature sensor Python library [3], enter:
sudo pip install Adafruit_DHT
When I'm doing Pi projects with my kids, I like to use prototyping expansion tops (Figure 4). These tops start at around $6 [4]. Not only do they make the wiring cleaner, it's a lot easier to move the projects around for testing.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.