Programming with Python Turtle graphics

Turtle Thermometer

For the thermometer project (Listing 3), I use two turtles: a static background turtle (thermo, line 27) and a dynamic turtle (bar, line 29). The background turtle is used only once at the start of the program. The drawing and text from the dynamic turtle (bar) are erased with the bar.clear method (line 8) and then redrawn with each new temperature value.

Listing 3


The final turtle thermometer code is a little more complex than the earlier code, because I include temperature labels (lines 42-46) and a drawbar() function (lines 6-19) that gets a new temperature value and redraws the dynamic bar and the text that indicates the current value (Figure 5).

Figure 5: Rasp Pi Turtle thermometer.

A screen object wn (line 22) resizes the window, adds a title, and defines an ontimer function (lines 19 and 58). The wn.ontimer(drawbar, 5000) statement updates the thermometer graphic every five seconds.

Final Comments

The mercury thermometer project was a fun example of how to use some basic Python Turtle code to talk to Raspberry Pi inputs. To include user input in your projects, the turtle library has an onkey(mykeyfunc,key) method to listen for key presses and an onscreenclick(onclick_handler) call to listen for mouse clicks.

Compared with other Python graphics libraries (e.g., Pygame, Tkinter, or Qt), Turtle graphics are slow and perhaps limiting, but for kids projects, Turtle can be a nice, easy introduction to graphics programming.

Raspberry Pi Turtle projects can be fun. Some other projects my daughters and I have created include:

  • Drawing pictures with a Wii remote
  • Showing the path of a rover while it is being driven
  • Creating a "Spirograph" drawing by entering colors and turn angles

The Author

You can investigate more neat projects by Pete Metcalfe and his daughters at

Buy this article as PDF

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

Buy Linux Magazine

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