Python turtle and tkinter

Shape Shifter

Teach a turtle to draw a square, a triangle and a star, then fill them with colour.

Day 1 45 to 60 minutes 8 steps Key Stage 3
Start step 1

Python's turtle is a robot pen. You give it orders in code, such as go forward 100 steps or turn left, and it draws a line wherever it goes. Today you will teach it three shapes and finish with a gold star.

You will learn

  • Give the turtle orders with forward(), left() and right()
  • Repeat code with a for loop instead of typing it again
  • Change the pen's colour and thickness, and fill shapes in
  • Move the turtle to any spot with goto()

You will need

  • A computer with Thonny or IDLE, or this page's Run it here button
  • About 45 minutes

Words to know

turtle
A pen you move with code. It draws a line wherever it goes.
loop
Code that repeats. for i in range(4): runs the lines under it four times.
indent
The spaces at the start of a line. Indented lines belong to the loop above them.
coordinates
Two numbers for a place: x across, y up. The middle of the window is (0, 0).

Step by step

  1. Step 1: Start a new file

    Open Thonny. Click New, then Save, and call the file shapes.py.

    Save the new file as shapes.py.
    Watch out Not turtle.py! A file with that name stops the turtle working.
  2. Step 2: Wake up the turtle

    Type these lines into the editor and click Run. A window opens with the turtle in the middle, facing right. t.forward(100) moves it 100 steps forward, and it draws a line as it goes.

    shapes.pyPython
    import turtle t = turtle.Turtle()t.forward(100) turtle.done()
    The turtle draws a line 100 steps long, from the middle of the window to the right.
    Tip turtle.done() keeps the window open at the end. Keep it as the last line of every turtle program.
  3. Step 3: Turn a corner

    To draw a square, the turtle goes forward, turns left 90 degrees (a quarter turn), and does the same again: four times in all. Add the new lines and run it.

    New: lines 5 to 11.

    shapes.pyPython
    import turtle t = turtle.Turtle()t.forward(100)t.left(90)t.forward(100)t.left(90)t.forward(100)t.left(90)t.forward(100)t.left(90) turtle.done()
    Four sides and four quarter turns make a square.
  4. Step 4: Let a loop do the work

    That was a lot of typing for one square. A for loop repeats the lines inside it, and range(4) means four times. Replace the eight lines of moves and turns with this loop.

    New: lines 4 to 7.

    shapes.pyPython
    import turtle t = turtle.Turtle() for i in range(4):    t.forward(100)    t.left(90) turtle.done()
    The same square, drawn by a loop.
    Watch out The two lines inside the loop must be indented: press Tab, or type four spaces. Do not forget the colon at the end of the for line.
  5. Step 5: Change the pen

    Now give the turtle a new look. shape("turtle") swaps the arrow for a turtle, color("blue") changes the pen's colour and pensize(5) makes the line thicker.

    New: lines 4 to 6.

    shapes.pyPython
    import turtle t = turtle.Turtle()t.shape("turtle")t.color("blue")t.pensize(5) for i in range(4):    t.forward(100)    t.left(90) turtle.done()
    A thick blue square, drawn by a turtle.
    Tip Python uses the American spelling: color, not colour.
  6. Step 6: Fill it with colour

    color can take two colours: the first is for the pen, the second fills the shape. Put begin_fill() before the loop and end_fill() after it, and the square is filled in when it is finished.

    New: lines 5, 8 and 12.

    shapes.pyPython
    import turtle t = turtle.Turtle()t.shape("turtle")t.color("blue", "lightblue")t.pensize(5) t.begin_fill()for i in range(4):    t.forward(100)    t.left(90)t.end_fill() turtle.done()
    A light blue square with a blue edge.
  7. Step 7: Move without drawing

    To draw somewhere else, lift the pen with penup(), goto the new spot, then put the pen down again with pendown(). The middle of the window is goto(0, 0): a negative x is to the left of it, and a negative y is below it.

    Move the square over to the left, then add a green triangle: three sides, turning 120 degrees each time. Lines starting with # are comments, notes for people that Python skips.

    New: lines 7 to 11 and 18 to 28.

    shapes.pyPython
    import turtle t = turtle.Turtle()t.shape("turtle")t.pensize(5) # a blue square on the leftt.penup()t.goto(-250, -50)t.pendown()t.color("blue", "lightblue")t.begin_fill()for i in range(4):    t.forward(100)    t.left(90)t.end_fill() # a green triangle in the middlet.penup()t.goto(-75, -50)t.pendown()t.color("green", "lightgreen")t.begin_fill()for i in range(3):    t.forward(100)    t.left(120)t.end_fill() turtle.done()
    The square has moved left, and a green triangle sits beside it.
  8. Step 8: Finish with a star

    A star is five lines, turning 144 degrees each time. Draw it on the right, then hide the turtle so it does not sit on your drawing.

    New: lines 29 to 40.

    shapes.pyPython
    import turtle t = turtle.Turtle()t.shape("turtle")t.pensize(5) # a blue square on the leftt.penup()t.goto(-250, -50)t.pendown()t.color("blue", "lightblue")t.begin_fill()for i in range(4):    t.forward(100)    t.left(90)t.end_fill() # a green triangle in the middlet.penup()t.goto(-75, -50)t.pendown()t.color("green", "lightgreen")t.begin_fill()for i in range(3):    t.forward(100)    t.left(120)t.end_fill() # a gold star on the rightt.penup()t.goto(100, 0)t.pendown()t.color("orange", "gold")t.begin_fill()for i in range(5):    t.forward(150)    t.right(144)t.end_fill() t.hideturtle()turtle.done()
    Three filled shapes: a square, a triangle and a star.
    Tip On some computers the middle of the star stays white. That is how Python fills a shape whose lines cross over each other.

Test it

Tick each one when it works. If one does not, look back at the step it comes from.

Stretch challenges

Finished early? Try these. Each one has a hint if you get stuck.

  1. Shape collector

    Draw a purple hexagon, which has six sides. How far must the turtle turn at each corner?

    Show a hint

    The turns of any shape add up to 360 degrees. Six sides means 360 ÷ 6 = 60 degrees a turn.

    Show one answer
    shapes.pyPython
    import turtle t = turtle.Turtle()t.color("purple", "plum")t.begin_fill()for i in range(6):    t.forward(80)    t.left(60)t.end_fill() turtle.done()
  2. Shape factory

    Ask the user how many sides they want, then draw that shape. Try 3, 5, 8 and 20 sides.

    Show a hint

    sides = int(input("How many sides? ")) asks a question and turns the answer into a whole number. Use range(sides), and turn 360 / sides each time.

    Show one answer
    shapes.pyPython
    import turtle sides = int(input("How many sides? ")) t = turtle.Turtle()for i in range(sides):    t.forward(80)    t.left(360 / sides) turtle.done()
  3. Star field

    Fill the window with ten stars of different colours, each in a random place.

    Show a hint

    Put the star's code inside another loop that runs ten times. import random at the top, then random.randint(-250, 250) picks a whole number between those two, and random.choice(colours) picks an item from a list.

    Show one answer
    shapes.pyPython
    import turtleimport random t = turtle.Turtle()t.speed(0)t.hideturtle()colours = ["red", "gold", "orange", "purple", "blue"] for n in range(10):    t.penup()    t.goto(random.randint(-250, 250), random.randint(-200, 200))    t.pendown()    t.color(random.choice(colours))    t.begin_fill()    for i in range(5):        t.forward(40)        t.right(144)    t.end_fill() turtle.done()

The finished code

Show the whole program
shapes.pyPython
import turtle t = turtle.Turtle()t.shape("turtle")t.pensize(5) # a blue square on the leftt.penup()t.goto(-250, -50)t.pendown()t.color("blue", "lightblue")t.begin_fill()for i in range(4):    t.forward(100)    t.left(90)t.end_fill() # a green triangle in the middlet.penup()t.goto(-75, -50)t.pendown()t.color("green", "lightgreen")t.begin_fill()for i in range(3):    t.forward(100)    t.left(120)t.end_fill() # a gold star on the rightt.penup()t.goto(100, 0)t.pendown()t.color("orange", "gold")t.begin_fill()for i in range(5):    t.forward(150)    t.right(144)t.end_fill() t.hideturtle()turtle.done()
Notes for the teacher

Timing

  1. 5 minutes: open Thonny and save shapes.py.
  2. 25 minutes: steps 2 to 6.
  3. 15 minutes: steps 7 and 8, then the tests.
  4. Any time left: the stretch challenges.

Mistakes to look out for

  1. A file saved as turtle.py. Python imports the student's own file instead of the turtle module and stops with AttributeError: partially initialized module 'turtle' has no attribute 'Turtle'. Rename the file, and delete any turtle.py left in the same folder.
  2. colour for color: AttributeError: 'Turtle' object has no attribute 'colour'.
  3. No colon after for i in range(4), or the lines under it not indented: a SyntaxError or an IndentationError.
  4. begin_fill() with no end_fill(): the shape is drawn but never filled.
  5. A window that closes at once, or stops responding: check that turtle.done() is the last line.

Going further

The turn at each corner of a regular shape is 360 ÷ the number of sides, which links well to angles in maths. The star turns 144 degrees because the turtle goes round twice while drawing it: 720 ÷ 5 = 144.