Python turtle and tkinter
Shape Shifter
Teach a turtle to draw a square, a triangle and a star, then fill them with colour.
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()andright() - Repeat code with a
forloop 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
Step 1: Start a new file
Open Thonny. Click New, then Save, and call the file
shapes.py.NewLoadSaveRunDebugStep overStep intoStep outResumeStopshapes.py1Shell>>>DocumentsFile name:shapes.pySave as type:Python files (*.py)SaveCancelSave the new file as shapes.py. Watch out Notturtle.py! A file with that name stops the turtle working.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.1import turtle2 3t = turtle.Turtle()4t.forward(100)5 6turtle.done()The turtle draws a line 100 steps long, from the middle of the window to the right. Tipturtle.done()keeps the window open at the end. Keep it as the last line of every turtle program.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.
1import turtle2 3t = turtle.Turtle()4t.forward(100)5t.left(90)6t.forward(100)7t.left(90)8t.forward(100)9t.left(90)10t.forward(100)11t.left(90)12 13turtle.done()Four sides and four quarter turns make a square. Step 4: Let a loop do the work
That was a lot of typing for one square. A
forloop repeats the lines inside it, andrange(4)means four times. Replace the eight lines of moves and turns with this loop.New: lines 4 to 7.
1import turtle2 3t = turtle.Turtle()4 5for i in range(4):6 t.forward(100)7 t.left(90)8 9turtle.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 theforline.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 andpensize(5)makes the line thicker.New: lines 4 to 6.
1import turtle2 3t = turtle.Turtle()4t.shape("turtle")5t.color("blue")6t.pensize(5)7 8for i in range(4):9 t.forward(100)10 t.left(90)11 12turtle.done()A thick blue square, drawn by a turtle. Tip Python uses the American spelling:color, notcolour.Step 6: Fill it with colour
colorcan take two colours: the first is for the pen, the second fills the shape. Putbegin_fill()before the loop andend_fill()after it, and the square is filled in when it is finished.New: lines 5, 8 and 12.
1import turtle2 3t = turtle.Turtle()4t.shape("turtle")5t.color("blue", "lightblue")6t.pensize(5)7 8t.begin_fill()9for i in range(4):10 t.forward(100)11 t.left(90)12t.end_fill()13 14turtle.done()A light blue square with a blue edge. Step 7: Move without drawing
To draw somewhere else, lift the pen with
penup(),gotothe new spot, then put the pen down again withpendown(). The middle of the window isgoto(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.
1import turtle2 3t = turtle.Turtle()4t.shape("turtle")5t.pensize(5)6 7# a blue square on the left8t.penup()9t.goto(-250, -50)10t.pendown()11t.color("blue", "lightblue")12t.begin_fill()13for i in range(4):14 t.forward(100)15 t.left(90)16t.end_fill()17 18# a green triangle in the middle19t.penup()20t.goto(-75, -50)21t.pendown()22t.color("green", "lightgreen")23t.begin_fill()24for i in range(3):25 t.forward(100)26 t.left(120)27t.end_fill()28 29turtle.done()The square has moved left, and a green triangle sits beside it. 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.
1import turtle2 3t = turtle.Turtle()4t.shape("turtle")5t.pensize(5)6 7# a blue square on the left8t.penup()9t.goto(-250, -50)10t.pendown()11t.color("blue", "lightblue")12t.begin_fill()13for i in range(4):14 t.forward(100)15 t.left(90)16t.end_fill()17 18# a green triangle in the middle19t.penup()20t.goto(-75, -50)21t.pendown()22t.color("green", "lightgreen")23t.begin_fill()24for i in range(3):25 t.forward(100)26 t.left(120)27t.end_fill()28 29# a gold star on the right30t.penup()31t.goto(100, 0)32t.pendown()33t.color("orange", "gold")34t.begin_fill()35for i in range(5):36 t.forward(150)37 t.right(144)38t.end_fill()39 40t.hideturtle()41turtle.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.
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
1import turtle2 3t = turtle.Turtle()4t.color("purple", "plum")5t.begin_fill()6for i in range(6):7 t.forward(80)8 t.left(60)9t.end_fill()10 11turtle.done()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. Userange(sides), and turn360 / sideseach time.Show one answer
1import turtle2 3sides = int(input("How many sides? "))4 5t = turtle.Turtle()6for i in range(sides):7 t.forward(80)8 t.left(360 / sides)9 10turtle.done()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 randomat the top, thenrandom.randint(-250, 250)picks a whole number between those two, andrandom.choice(colours)picks an item from a list.Show one answer
1import turtle2import random3 4t = turtle.Turtle()5t.speed(0)6t.hideturtle()7colours = ["red", "gold", "orange", "purple", "blue"]8 9for n in range(10):10 t.penup()11 t.goto(random.randint(-250, 250), random.randint(-200, 200))12 t.pendown()13 t.color(random.choice(colours))14 t.begin_fill()15 for i in range(5):16 t.forward(40)17 t.right(144)18 t.end_fill()19 20turtle.done()
The finished code
Show the whole program
1import turtle2 3t = turtle.Turtle()4t.shape("turtle")5t.pensize(5)6 7# a blue square on the left8t.penup()9t.goto(-250, -50)10t.pendown()11t.color("blue", "lightblue")12t.begin_fill()13for i in range(4):14 t.forward(100)15 t.left(90)16t.end_fill()17 18# a green triangle in the middle19t.penup()20t.goto(-75, -50)21t.pendown()22t.color("green", "lightgreen")23t.begin_fill()24for i in range(3):25 t.forward(100)26 t.left(120)27t.end_fill()28 29# a gold star on the right30t.penup()31t.goto(100, 0)32t.pendown()33t.color("orange", "gold")34t.begin_fill()35for i in range(5):36 t.forward(150)37 t.right(144)38t.end_fill()39 40t.hideturtle()41turtle.done()Notes for the teacher
Timing
- 5 minutes: open Thonny and save
shapes.py. - 25 minutes: steps 2 to 6.
- 15 minutes: steps 7 and 8, then the tests.
- Any time left: the stretch challenges.
Mistakes to look out for
- 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 anyturtle.pyleft in the same folder. colourforcolor: AttributeError: 'Turtle' object has no attribute 'colour'.- No colon after
for i in range(4), or the lines under it not indented: a SyntaxError or an IndentationError. begin_fill()with noend_fill(): the shape is drawn but never filled.- 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.