Programming with Python Turtle graphics
Turtles
![© Lead Image © Baurzhan Moldabekov, 123RF.com © Lead Image © Baurzhan Moldabekov, 123RF.com](/var/linux_magazin/storage/images/issues/2020/231/python-turtles/123rf_83083752_steampunk-turtle_baurzhanmoldabekov_resized.png/761687-1-eng-US/123rf_83083752_Steampunk-turtle_BaurzhanMoldabekov_resized.png_medium.png)
© Lead Image © Baurzhan Moldabekov, 123RF.com
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
![](/var/linux_magazin/storage/images/issues/2020/231/python-turtles/figure-3/761696-1-eng-US/Figure-3_large.png)
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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.
-
SUSE Offers CentOS 7 Support with Liberty Linux Lite
SUSE's Liberty Linux support offering now includes CentOS 7, which means businesses won't be forced to migrate those servers for some time.
-
Ubuntu's App Center Finally Supports Local Installs Again
If you regularly download .deb files and would prefer a GUI method of installing, Ubuntu has your back.
-
AlmaLinux Now Supports Raspberry Pi 5
If you're looking to create with the Raspberry Pi 5 and want to use AlmaLinux as your OS, you're in luck because it's now possible.
-
Kubuntu Focus Releases New Iterations of Ir14 and Ir16 Laptops
If you're a fan of the Kubuntu Focus laptops or have been waiting for the right time to purchase one, that time might be now.
-
NixOS 24.05 Is Ready for Prime Time
The latest release of NixOS (Uakari) has arrived and offers its usual reproducible, declarative, and reliable goodness.
-
Linux Lite 7.0 Officially Released
Based on Ubuntu 24.04 and kernel 6.8, Linux Lite version 7 now offers more options than ever.
-
KaOS Linux 2024.05 Adds Bcachfs Support and More
With updates all around, KaOS Linux now includes support for the bcachefs file system.
-
TUXEDO Computers Unveils New Iteration of the Stellaris Laptop Line
The Stellaris Slim 15 is the 6th generation and includes either an AMD or Intel CPU