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
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.