Python turtle and tkinter
Build a Town
Write your own turtle commands, called functions, and use them to build a street of houses.
Drawing a house takes a lot of code. Drawing ten houses would take ten times as much. A function is a command you make yourself: write the code once, give it a name, and use the name whenever you need it. Today you will make a rectangle, a triangle, a house, a tree and a sun, then build a town with them.
You will learn
- Make your own commands with
def - Give a function parameters, so it can draw different things
- Use functions inside other functions
- Plan a picture with x and y coordinates
You will need
- A computer with Thonny or IDLE, or this page's Run it here button
- Day 1's project, for
gotoand filling shapes
Words to know
- function
- A command you make yourself, with
def. Write it once, use it many times. - parameter
- A name in a function's brackets that takes a different value each time it is used.
- call
- Using a function:
house(-250, -50, "tomato"). - decomposition
- Breaking a big problem into smaller ones: a town into houses, a house into shapes.
Step by step
Step 1: Start a new file with a sky
Click New, save the file as
town.py, and type this.speed(0)makes the turtle draw at full speed, and the background is the colour of the sky.1import turtle2 3t = turtle.Turtle()4t.speed(0)5turtle.bgcolor("skyblue")6 7turtle.done()An empty blue sky, with the turtle in the middle. Step 2: Make a rectangle command
defdefines a new function calledrectangle. The names in brackets are its parameters: the width, the height and the colour, which are different every time you use it. The code indented underdefis what the function does. It only runs when you call the function, as the last new line does.New: lines 7 to 18.
1import turtle2 3t = turtle.Turtle()4t.speed(0)5turtle.bgcolor("skyblue")6 7def rectangle(width, height, colour):8 t.color("black", colour)9 t.begin_fill()10 for i in range(2):11 t.forward(width)12 t.left(90)13 t.forward(height)14 t.left(90)15 t.end_fill()16 17rectangle(100, 80, "tomato")18 19turtle.done()rectangle(100, 80, "tomato") draws a red rectangle 100 wide and 80 high. Watch out The function must come before the line that calls it. Python reads from the top down.Step 3: Lay the ground
Moving without drawing takes three lines, so make that a function too:
move(x, y). Then use both functions to lay a wide green strip of ground at the bottom. Delete the red rectangle line.New: lines 17 to 23.
1import turtle2 3t = turtle.Turtle()4t.speed(0)5turtle.bgcolor("skyblue")6 7def rectangle(width, height, colour):8 t.color("black", colour)9 t.begin_fill()10 for i in range(2):11 t.forward(width)12 t.left(90)13 t.forward(height)14 t.left(90)15 t.end_fill()16 17def move(x, y):18 t.penup()19 t.goto(x, y)20 t.pendown()21 22move(-400, -150)23rectangle(800, 100, "green")24 25turtle.done()A strip of green ground across the bottom of the sky. Step 4: Add a roof
Add a
trianglefunction, then draw the walls of a house standing on the ground, and a brown roof on top of the walls. The ground's top edge is at y = 50 below the middle, so the walls start atmove(-250, -50).New: lines 22 to 29 and 33 to 37.
1import turtle2 3t = turtle.Turtle()4t.speed(0)5turtle.bgcolor("skyblue")6 7def rectangle(width, height, colour):8 t.color("black", colour)9 t.begin_fill()10 for i in range(2):11 t.forward(width)12 t.left(90)13 t.forward(height)14 t.left(90)15 t.end_fill()16 17def move(x, y):18 t.penup()19 t.goto(x, y)20 t.pendown()21 22def triangle(size, colour):23 t.color("black", colour)24 t.begin_fill()25 for i in range(3):26 t.forward(size)27 t.left(120)28 t.end_fill()29 30move(-400, -150)31rectangle(800, 100, "green")32 33move(-250, -50)34rectangle(100, 80, "tomato")35move(-250, 30)36triangle(100, "brown")37 38turtle.done()Walls and a roof: one house. Step 5: A house command
Now wrap the walls, the roof and a door into one function called
house. Its parameters say where the house goes and what colour its walls are.y + 80is 80 above the bottom of the walls, the top of the walls, where the roof goes. Replace the four lines that drew the house with one call tohouse.New: lines 30 to 37 and 41.
1import turtle2 3t = turtle.Turtle()4t.speed(0)5turtle.bgcolor("skyblue")6 7def rectangle(width, height, colour):8 t.color("black", colour)9 t.begin_fill()10 for i in range(2):11 t.forward(width)12 t.left(90)13 t.forward(height)14 t.left(90)15 t.end_fill()16 17def move(x, y):18 t.penup()19 t.goto(x, y)20 t.pendown()21 22def triangle(size, colour):23 t.color("black", colour)24 t.begin_fill()25 for i in range(3):26 t.forward(size)27 t.left(120)28 t.end_fill()29 30def house(x, y, colour):31 move(x, y)32 rectangle(100, 80, colour)33 move(x, y + 80)34 triangle(100, "brown")35 move(x + 40, y)36 rectangle(20, 40, "saddlebrown")37 38move(-400, -150)39rectangle(800, 100, "green")40 41house(-250, -50, "tomato")42 43turtle.done()One call to house() draws the walls, the roof and the door. Step 6: Build a street
This is where functions pay off: two more houses take two more lines.
New: lines 42 and 43.
1import turtle2 3t = turtle.Turtle()4t.speed(0)5turtle.bgcolor("skyblue")6 7def rectangle(width, height, colour):8 t.color("black", colour)9 t.begin_fill()10 for i in range(2):11 t.forward(width)12 t.left(90)13 t.forward(height)14 t.left(90)15 t.end_fill()16 17def move(x, y):18 t.penup()19 t.goto(x, y)20 t.pendown()21 22def triangle(size, colour):23 t.color("black", colour)24 t.begin_fill()25 for i in range(3):26 t.forward(size)27 t.left(120)28 t.end_fill()29 30def house(x, y, colour):31 move(x, y)32 rectangle(100, 80, colour)33 move(x, y + 80)34 triangle(100, "brown")35 move(x + 40, y)36 rectangle(20, 40, "saddlebrown")37 38move(-400, -150)39rectangle(800, 100, "green")40 41house(-250, -50, "tomato")42house(-100, -50, "gold")43house(150, -50, "violet")44 45turtle.done()Three houses, each a different colour. Step 7: Plant some trees
A tree is a brown trunk with a green circle on top.
circle(30)draws a circle with the turtle on its bottom edge, so it sits on top of the trunk.New: lines 38 to 46, 53 and 54.
1import turtle2 3t = turtle.Turtle()4t.speed(0)5turtle.bgcolor("skyblue")6 7def rectangle(width, height, colour):8 t.color("black", colour)9 t.begin_fill()10 for i in range(2):11 t.forward(width)12 t.left(90)13 t.forward(height)14 t.left(90)15 t.end_fill()16 17def move(x, y):18 t.penup()19 t.goto(x, y)20 t.pendown()21 22def triangle(size, colour):23 t.color("black", colour)24 t.begin_fill()25 for i in range(3):26 t.forward(size)27 t.left(120)28 t.end_fill()29 30def house(x, y, colour):31 move(x, y)32 rectangle(100, 80, colour)33 move(x, y + 80)34 triangle(100, "brown")35 move(x + 40, y)36 rectangle(20, 40, "saddlebrown")37 38def tree(x, y):39 move(x, y)40 rectangle(16, 40, "saddlebrown")41 move(x + 8, y + 40)42 t.color("darkgreen")43 t.begin_fill()44 t.circle(30)45 t.end_fill()46 47move(-400, -150)48rectangle(800, 100, "green")49 50house(-250, -50, "tomato")51house(-100, -50, "gold")52house(150, -50, "violet")53tree(50, -50)54tree(300, -50)55 56turtle.done()Two trees between the houses. Step 8: Here comes the sun
Add a
sunfunction and put the sun in the sky. Hide the turtle at the end.New: lines 47 to 53, 62 and 64.
1import turtle2 3t = turtle.Turtle()4t.speed(0)5turtle.bgcolor("skyblue")6 7def rectangle(width, height, colour):8 t.color("black", colour)9 t.begin_fill()10 for i in range(2):11 t.forward(width)12 t.left(90)13 t.forward(height)14 t.left(90)15 t.end_fill()16 17def move(x, y):18 t.penup()19 t.goto(x, y)20 t.pendown()21 22def triangle(size, colour):23 t.color("black", colour)24 t.begin_fill()25 for i in range(3):26 t.forward(size)27 t.left(120)28 t.end_fill()29 30def house(x, y, colour):31 move(x, y)32 rectangle(100, 80, colour)33 move(x, y + 80)34 triangle(100, "brown")35 move(x + 40, y)36 rectangle(20, 40, "saddlebrown")37 38def tree(x, y):39 move(x, y)40 rectangle(16, 40, "saddlebrown")41 move(x + 8, y + 40)42 t.color("darkgreen")43 t.begin_fill()44 t.circle(30)45 t.end_fill()46 47def sun(x, y):48 move(x, y)49 t.color("orange", "yellow")50 t.begin_fill()51 t.circle(40)52 t.end_fill()53 54move(-400, -150)55rectangle(800, 100, "green")56 57house(-250, -50, "tomato")58house(-100, -50, "gold")59house(150, -50, "violet")60tree(50, -50)61tree(300, -50)62sun(250, 150)63 64t.hideturtle()65turtle.done()The finished town: three houses, two trees and the sun.
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.
Windows
Add two windows to the
housefunction, so every house gets them at once.Show a hint
Inside
house,moveto a spot inside the walls and draw a smallrectangle(20, 20, "lightyellow"). Trymove(x + 10, y + 45)andmove(x + 70, y + 45).A long street
Use a loop to draw a street of six houses, each a different colour.
Show a hint
Make a list of six colours. Then
for i in range(6):andhouse(-380 + i * 130, -50, colours[i])spaces the houses 130 steps apart.Show one answer
1import turtle2 3t = turtle.Turtle()4t.speed(0)5turtle.bgcolor("skyblue")6 7def rectangle(width, height, colour):8 t.color("black", colour)9 t.begin_fill()10 for i in range(2):11 t.forward(width)12 t.left(90)13 t.forward(height)14 t.left(90)15 t.end_fill()16 17def move(x, y):18 t.penup()19 t.goto(x, y)20 t.pendown()21 22def triangle(size, colour):23 t.color("black", colour)24 t.begin_fill()25 for i in range(3):26 t.forward(size)27 t.left(120)28 t.end_fill()29 30def house(x, y, colour):31 move(x, y)32 rectangle(100, 80, colour)33 move(x, y + 80)34 triangle(100, "brown")35 move(x + 40, y)36 rectangle(20, 40, "saddlebrown")37 38move(-400, -150)39rectangle(800, 100, "green")40 41colours = ["tomato", "gold", "violet", "orange", "pink", "lightgreen"]42for i in range(6):43 house(-380 + i * 130, -50, colours[i])44 45t.hideturtle()46turtle.done()Clouds
Write a
cloud(x, y)function from three white circles side by side, and put clouds in the sky.Show a hint
Draw three filled white circles, each one 30 steps to the right of the last:
move(x, y),move(x + 30, y)andmove(x + 60, y).
The finished code
Show the whole program
1import turtle2 3t = turtle.Turtle()4t.speed(0)5turtle.bgcolor("skyblue")6 7def rectangle(width, height, colour):8 t.color("black", colour)9 t.begin_fill()10 for i in range(2):11 t.forward(width)12 t.left(90)13 t.forward(height)14 t.left(90)15 t.end_fill()16 17def move(x, y):18 t.penup()19 t.goto(x, y)20 t.pendown()21 22def triangle(size, colour):23 t.color("black", colour)24 t.begin_fill()25 for i in range(3):26 t.forward(size)27 t.left(120)28 t.end_fill()29 30def house(x, y, colour):31 move(x, y)32 rectangle(100, 80, colour)33 move(x, y + 80)34 triangle(100, "brown")35 move(x + 40, y)36 rectangle(20, 40, "saddlebrown")37 38def tree(x, y):39 move(x, y)40 rectangle(16, 40, "saddlebrown")41 move(x + 8, y + 40)42 t.color("darkgreen")43 t.begin_fill()44 t.circle(30)45 t.end_fill()46 47def sun(x, y):48 move(x, y)49 t.color("orange", "yellow")50 t.begin_fill()51 t.circle(40)52 t.end_fill()53 54move(-400, -150)55rectangle(800, 100, "green")56 57house(-250, -50, "tomato")58house(-100, -50, "gold")59house(150, -50, "violet")60tree(50, -50)61tree(300, -50)62sun(250, 150)63 64t.hideturtle()65turtle.done()Notes for the teacher
Timing
- 10 minutes: steps 1 and 2. Spend time on the idea of parameters: the same function, different values.
- 25 minutes: steps 3 to 6.
- 15 minutes: steps 7 and 8, then the tests.
Mistakes to look out for
- Calling a function before its
def: NameError: name 'house' is not defined. Functions go at the top. - Forgetting the brackets on a call:
houseon its own does nothing at all. - The wrong number of values in a call: TypeError: house() missing 1 required positional argument: 'colour'.
- Code indented under
defby mistake, such as the calls at the bottom: then nothing is drawn, because the calls are now inside a function that is never called.
Going further
This is decomposition in action: a town is broken into houses, a house into rectangles and triangles. Ask students to sketch their town on squared paper first, with coordinates, before they code it.