Python Turtle Graphics and Tkinter GUI Programming

This is an intermediate level Python programming lesson. It discusses the relationship between the Python Turtle Graphics module and Python Tkinter GUI programming. If you are not yet an intermediate Python programmer, we have other lessons on this site specifically about Python Turtle Graphics which are likely to be more suitable.

The Python Turtle Graphics Module is actually built on top of Tkinter, which is a more powerful, general purpose Python library for building GUIs (Graphical User Interfaces).

All of the Turtle methods you are probably familiar with make use of underlying Tkinter methods. For example, the following program creates a screen where we can place turtle objects, and enables us to control attributes of that screen such as its size, title, color etc. It is not essential to explicitly create a screen object when working with turtle graphics, but it is very useful to do so sometimes. The screen that we create using this code is actually making use of Tkinter widgets under the hood. We will learn more about which ones shortly.

Python Code Listing for Creating a Turtle Graphics Screen

Taking Python Turtle Graphics to the Next Level

Once you have worked with Python Turtle Graphics for a while, you may find that there are things you would like to do in your programs that are difficult or seemingly impossible with just the available turtle commands. One common example of this for me is creating buttons. This can be done using just turtle methods, but it’s a bit of a faff, and there is a much easier way using a little tkinter to supercharge your turtle program. An example of using tkinter to make a button insider a turtle program is given below.

Using a Tkinter Button inside a Python Turtle Program

A few comments to help you to understand this code:

  • screen is an instance of turtle.Screen()
  • As in the previous example canvas gives us access to the underlying tkinter canvas where our turtles live and play.
  • button is a tkinter widget. It is placed on the screen by the first argument canvas.master, which references the parent element of the canvas
  • There are several “layers” at play here. Don’t worry if you don’t understand them all at this point. Clarity will come with experience.
  • One new “trick” here is the use of canvas.create_window(-200, -200, window=button) to place the button on the canvas.

Personally I think that this is an ideal combination of programming power and simplicity, and would suggest that learners spend a fair bit of time writing programs (including many fun games) using the “turbo-charged Turtle” approach.

However, there is a place for a more full-fledged use of tkinter, and using Turtle Graphics in embedded mode.

Python turtle operates in two modes: standalone, and embedded in a larger tkinter program. Instead of turtle.Turtle and turtle.Screen, when using turtle embedded, you work with turtle.RawTurtle, and turtle.TurtleScreen. You build your tkinter interface as needed, and use a Canvas to contain your turtle graphics.

To illustrate the difference between these two approaches, I have provided basically the same program as above, but this time using turtle in embedded mode. The only significant difference between this an the other program is the placement of the button.

Basic Python Turtle Embedded in Tkinter Program

Several of the components of the above code have already been explained above for the standalone version The difference here is that we are explicitly using tkinter objects and methods, rather than the turtle equivalents which call them anyway, but provide a more beginner friendly interface.

Object Oriented Programming Version of Embedded Turtle Graphics Tkinter Program

Finally, is is considered best practice to use an Object Oriented Programming style when working with Python tkinter. Therefore I have provided the code for that approach as well. OOP is beyond the scope of this article, but if you are familiar with it, it can be very informative to see how it applies to a tkinter application. Here is the Python listing for the OOP version:

That’s it for now. In this article we have covered how to extend Python Turtle Graphics with methods from the Python Tkinter GUI Library, and how to use Turtle Graphics in embedded mode in a Tkinter application.

Happy computing!

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Join our mailing list

Join our mailing list to receive awesome articles about learning Python and Computer Science in a fun and accessible way, straight to your inbox.

 Take your Python skills to the next level!

with our free email course on object oriented programming with Python