Python turtle and tkinter
Spiral Party
Make the turtle spin rainbow spirals that grow and twist across a black sky.
Yesterday every line was the same length. Today each line will be a little longer than the one before, so the turtle spirals outwards. Tiny changes to one number make wild new patterns, so keep experimenting.
You will learn
- Use the loop counter
ito make each line longer than the last - Store colours in a list and pick them in turn
- Change the speed, the background and the title of the window
- Write words in the turtle's window
You will need
- A computer with Thonny or IDLE, or this page's Run it here button
- Day 1's project helps, but you can start here
Words to know
- variable
- A name that stores a value. In the loop,
istores the number of the line. - list
- Many values in one variable:
["red", "orange"]. The first item is number 0. - remainder
- What is left over after dividing.
i % 6is the remainder ofidivided by 6. - speed
- How fast the turtle moves: 1 is slowest, 10 is fast and 0 is fastest of all.
Step by step
Step 1: Start a new file
Open Thonny, click New and save the file as
spirals.py.NewLoadSaveRunDebugStep overStep intoStep outResumeStopspirals.py1Shell>>>DocumentsFile name:spirals.pySave as type:Python files (*.py)SaveCancelSave the new file as spirals.py. Step 2: A square spiral
In
for i in range(100), the variableicounts 0, 1, 2 and so on up to 99, one number each time round the loop. Soforward(i * 2)makes every line 2 steps longer than the one before.speed(0)is the turtle's fastest speed, so you do not have to wait.1import turtle2 3t = turtle.Turtle()4t.speed(0)5 6for i in range(100):7 t.forward(i * 2)8 t.left(90)9 10turtle.done()Each line is a little longer, so the square turns into a spiral. Step 3: Give it a twist
Change the turn from 90 degrees to 91. One degree more makes every corner miss the last one slightly, and the spiral twists.
New: line 8.
1import turtle2 3t = turtle.Turtle()4t.speed(0)5 6for i in range(100):7 t.forward(i * 2)8 t.left(91)9 10turtle.done()91 degree turns: the square spiral twists round. Step 4: Turn on the party lights
Make the background black with
turtle.bgcolor("black"), and keep six colours in a list.i % 6is the remainder wheniis divided by 6, which goes 0, 1, 2, 3, 4, 5, 0, 1, 2... socolours[i % 6]picks the colours in turn, again and again.New: lines 3, 6 and 9.
1import turtle2 3turtle.bgcolor("black")4t = turtle.Turtle()5t.speed(0)6colours = ["red", "orange", "yellow", "green", "blue", "purple"]7 8for i in range(100):9 t.color(colours[i % 6])10 t.forward(i * 2)11 t.left(91)12 13turtle.done()Each line takes the next colour from the list. Step 5: Make it a hexagon
A hexagon turns 60 degrees at each corner, so 59 makes a twisting hexagon. Change the turn to 59 and the loop to 120 times. With six colours and six sides, each colour lines up on its own side.
New: lines 8 and 11.
1import turtle2 3turtle.bgcolor("black")4t = turtle.Turtle()5t.speed(0)6colours = ["red", "orange", "yellow", "green", "blue", "purple"]7 8for i in range(120):9 t.color(colours[i % 6])10 t.forward(i * 2)11 t.left(59)12 13turtle.done()Six colours, six sides: a rainbow hexagon spiral. Step 6: Thicker and thicker
pensizecan change inside the loop too.i / 30 + 1starts at 1 and grows to 5 by the end, so the lines get thicker as the spiral grows.New: line 10.
1import turtle2 3turtle.bgcolor("black")4t = turtle.Turtle()5t.speed(0)6colours = ["red", "orange", "yellow", "green", "blue", "purple"]7 8for i in range(120):9 t.color(colours[i % 6])10 t.pensize(i / 30 + 1)11 t.forward(i * 2)12 t.left(59)13 14turtle.done()The outer lines are thicker than the inner ones. Step 7: Sign your work
Give the window a title, hide the turtle, then lift the pen, go to the top of the window and
writea message.align="center"puts the middle of the words at the turtle, andfontsets the font, the size and the style.New: lines 4 and 15 to 20.
1import turtle2 3turtle.bgcolor("black")4turtle.title("Spiral Party")5t = turtle.Turtle()6t.speed(0)7colours = ["red", "orange", "yellow", "green", "blue", "purple"]8 9for i in range(120):10 t.color(colours[i % 6])11 t.pensize(i / 30 + 1)12 t.forward(i * 2)13 t.left(59)14 15t.hideturtle()16t.penup()17t.goto(0, 260)18t.color("white")19t.write("Spiral Party!", align="center", font=("Arial", 28, "bold"))20 21turtle.done()The finished Spiral Party, with its title.
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.
Angle explorer
Change 59 to other angles: try 89, 121, 144 and 175. Which makes your favourite pattern?
Show a hint
Angles just under 360 ÷ sides make that shape twist: 89 for squares, 119 for triangles, 71 for pentagons.
Circle flower
Draw 36 circles, turning 10 degrees after each one, to make a flower.
Show a hint
t.circle(100)draws a circle 100 steps across from the middle. 36 turns of 10 degrees make a full turn.Show one answer
1import turtle2 3turtle.bgcolor("black")4t = turtle.Turtle()5t.speed(0)6colours = ["red", "orange", "yellow", "green", "blue", "purple"]7 8for i in range(36):9 t.color(colours[i % 6])10 t.circle(100)11 t.left(10)12 13turtle.done()Your choice
Ask the user for the angle, then draw their spiral.
Show a hint
angle = int(input("Pick an angle: "))gets a whole number. Then uset.left(angle).Show one answer
1import turtle2 3angle = int(input("Pick an angle between 45 and 150: "))4 5turtle.bgcolor("black")6t = turtle.Turtle()7t.speed(0)8colours = ["red", "orange", "yellow", "green", "blue", "purple"]9 10for i in range(120):11 t.color(colours[i % 6])12 t.forward(i * 2)13 t.left(angle)14 15turtle.done()
The finished code
Show the whole program
1import turtle2 3turtle.bgcolor("black")4turtle.title("Spiral Party")5t = turtle.Turtle()6t.speed(0)7colours = ["red", "orange", "yellow", "green", "blue", "purple"]8 9for i in range(120):10 t.color(colours[i % 6])11 t.pensize(i / 30 + 1)12 t.forward(i * 2)13 t.left(59)14 15t.hideturtle()16t.penup()17t.goto(0, 260)18t.color("white")19t.write("Spiral Party!", align="center", font=("Arial", 28, "bold"))20 21turtle.done()Notes for the teacher
Timing
- 5 minutes: a new file, and the square spiral.
- 25 minutes: steps 3 to 6.
- 10 minutes: step 7 and the tests.
- The rest: the angle explorer is a good whole class finish. Ask students to share their best angle.
Mistakes to look out for
colours[i % 6]with a list of five colours: one colour is missed out, and with more than six the code crashes with IndexError: list index out of range. The 6 must match the number of colours in the list.turtle.bgcolorwritten ast.bgcolor: AttributeError. The background belongs to the screen, not the turtle.- Writing the message before hiding the turtle leaves it sitting on the words: hide it first.
Going further
% (modulo) is worth a minute at the board: 7 % 6 is 1, 12 % 6 is 0. It is the same idea as a clock going round, and it appears again in the micro:bit and web tracks.