Programming with Python Turtle graphics

Turtles

© Lead Image © Baurzhan Moldabekov, 123RF.com

© Lead Image © Baurzhan Moldabekov, 123RF.com

Article from Issue 231/2020
Author(s): , Author(s):

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

 

Figure 1: Three Python Turtle graphics.

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).

Figure 2: Thermometer background.

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

 

Figure 3: Fritzing diagram of DHT11 wiring to the Rasp Pi; the blue wire is signal (PP7), the red wire is 5V power (PP2), and the black wire is ground (GND, PP6).

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.

Figure 4: DHT11 wiring to the Rasp Pi through a prototyping board.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Workspace: Markdown Tools

    From note-taking applications to wikis – there are plenty of handy tools for working with Markdown-formatted content. Here are a few worth adding to your toolbox.

  • Perl – Memes

    It started off harmlessly enough with a few funny pictures of cats, but eventually it became the Internet phenomenon par excellence. It's no joke: Perl gives you some great tools for building and customizing memes yourself.

  • Gran Canaria Desktop Summit: Saints, Gentlemen and Schoolchildren

    At the start of the Gran Canaria Desktop Summit on the Canary Islands, Richard Stallman, Robert Lefkowitz and Walter Bender rallied open source developers in their common tasks.

  • Ren'Py

    Ren'Py helps you create Android, Linux, macOS, Windows, and HTML5 games and apps.

  • Xonsh

    Create lightweight Raspberry Pi scripts with Xonsh, a Python shell that lets you write scripts in Python with Bash commands mixed in.

comments powered by Disqus
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

News