Python - Drawing

It is possible to use Python to draw images on the screen. To do this we need to add (import) some new commands to Python. The group of commands we will import is called the turtle library.

The turtle library gives us a new set of command that allow us to move a sprite around a window and draw shapes. This is similar to what we did in Scratch when we used the pen tool to draw shapes on the stage by moving the sprite.

Important!

When saving your file NEVER call the file turtle.py or the program will not work

Take a look at the following code:

#add the drawing commands

import turtle as terry


# draw a line 100 pixels long

terry.forward(100)

It will add the new commands and create a turtle called terry. When 'terry' moves it will draw on the screen.

To get the turtle to turn, we need to say in which direction and by how many degrees.

For example the code terry.right(90) will turn the turtle 90° to the right.

Let's put a few commands together and draw a square...

Copy the code above.
Save the program as 'demoTurtle.py' and run it (F5).

Watch the video below to see how to draw with Python

pythonDrawing.mp4

Challenges

Write new programs that will get terry to draw:

  • a rectangle

  • a triangle

  • a pentagon

  • a hexagon

  • a simple house

  • your name

Bonus Challenge

  • Can you find out how to change the pen size and colour of your turtle?

Key Words

Library

A group of extra commands that can be added to your programming language

Turtle Graphics

A way of drawing images on a computer by moving a sprite (turtle) across the screen.

More Resources

You can find out more about drawing in Python by following this fun Turtle Graphics tutorial.

More specific information about the turtle library can be found on the Python website.