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
import turtle screen = turtle.Screen() screen.title("A Turtle Graphics Screen") screen.setup(600, 600) screen.bgcolor("cyan") turtle.done()
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
import turtle import tkinter as tk def do_stuff(): for color in ["red", "yellow", "green"]: my_lovely_turtle.color(color) my_lovely_turtle.right(120) def press(): do_stuff() if __name__ == "__main__": screen = turtle.Screen() screen.bgcolor("cyan") canvas = screen.getcanvas() button = tk.Button(canvas.master, text="Press me", command=press) canvas.create_window(-200, -200, window=button) my_lovely_turtle = turtle.Turtle(shape="turtle") turtle.done()
A few comments to help you to understand this code:
screenis an instance of
- As in the previous example
canvasgives us access to the underlying
tkintercanvas where our turtles live and play.
tkinterwidget. 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
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.Screen, when using turtle embedded, you work with
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
import turtle import tkinter as tk def do_stuff(): for color in ["red", "yellow", "green"]: my_lovely_turtle.color(color) my_lovely_turtle.right(120) def press(): do_stuff() if __name__ == "__main__": root = tk.Tk() canvas = tk.Canvas(root) canvas.config(width=600, height=200) canvas.pack(side=tk.LEFT) screen = turtle.TurtleScreen(canvas) screen.bgcolor("cyan") button = tk.Button(root, text="Press me", command=press) button.pack() my_lovely_turtle = turtle.RawTurtle(screen, shape="turtle") root.mainloop()
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:
import turtle import tkinter as tk class App: def __init__(self, master): self.master = master self.master.title("Raw Turtle") self.canvas = tk.Canvas(master) self.canvas.config(width=600, height=200) self.canvas.pack(side=tk.LEFT) self.screen = turtle.TurtleScreen(self.canvas) self.screen.bgcolor("cyan") self.button = tk.Button(self.master, text="Press me", command=self.press) self.button.pack() self.my_lovely_turtle = turtle.RawTurtle(self.screen, shape="turtle") self.my_lovely_turtle.color("green") def do_stuff(self): for color in ["red", "yellow", "green"]: self.my_lovely_turtle.color(color) self.my_lovely_turtle.right(120) def press(self): self.do_stuff() if __name__ == '__main__': root = tk.Tk() app = App(root) root.mainloop()
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.