Python Tutorial

Python Turtle Graphics – The Complete Guide.

Python turtle graphics is one of the cool ways to implement your knowledge in Python before you start writing complex programs to solve problems in life.

In this tutorial, I will make Python more practical for you and also fun.

I know that before you came across this tutorial, you were looking for fun things to do with Python.

So I will do my best to make every bit of this tutorial exciting.

let’s get to the basics now.

Don’t forget to:

How to Create a Turtle Window.

To create a turtle window on your computer screen, please open a new file editor window and type the following code snippet. Save it as Turtle_window.py

When you run this program, you will get this result on your screen.

python turtle window

Python Turtle Graphics Window

You can see an arrow head at the middle point of the window pointing to the east which is the default position. This location is called home.

The position of the turtle is specified with the two-dimensional coordinate system, (x,y).

The default position of the turtle(arrow head) is (0,0) which is the center of the window.

I will have to talk about some things that are important and it is for those that will use Python IDE (pycharm, spyder etc) for this tutorial.

When you run the code snippets of this tutorial in any Python IDE you will notice that the turtle window will open and close immediately.

To allow the turtle window stay so you can see the output of your program, include t.screen.mainloop() in your program. It should be the last statement of your program.

To make the turtle window disappear after viewing the turtle screen when you click on the turtle canvas, use the method screen.exitonclick() .

Include t.screen.exitonclick() before t.screen.mainloop().

The following code snippet shows you how you can use them.

When you run the code snippet above in any Python IDE such as Pycharm or Spyder, the turtle screen won’t disappear immediately unless you click on the turtle canvas.

How to Change the Turtle Window Background Color

The default background color is white which can be boring.

To change the color of the turtle window background, please open a new file editor window and type the following code snippet. Save it as Turtle_window_background.py

When you run this program, you will get this result on your screen.

python turtle window

Python Turtle Graphics Window

The background color of the turtle window is black but there is no turtle because the default color of the turtle is black.

Note :

The screen.bgcolor() method accepts one argument which is the color of the turtle window as a string.

The default color of Python turtle window is white. You can change it using the method screen.bgcolor()

How to Change the Color of the Turtle

To change the color of the turtle,

Type the following code snippet into a new file editor and save it as any name you like.

When you run this program, you will get this result on your screen.

python turtle custom window background

Blue turtle on a black canvas

t.color(“blue”) changes the color of the turtle to blue.

How to change the Background picture of the Turtle Screen.

Here’s a code snippet that changes the background image of the turtle screen( turtle_background_pic.py) .

Here’s the output of the program(turtle_background_pic.py)

how to change the turtle screen background image with turtle.screen.bgpic()

An Owl image on turtle canvas

Note:

  • Only the gif image format is accepted.
  • Let your program and the gif image be in the same folder on your computer.
  • I provided some gif images in the source code download file.

How to Set the Heading of the Turtle

By default the turtle faces the east, to change the direction in which the turtle is facing,

The syntax is,

t.setheading(angle)

Angle 90 sets the turtle to face the North.

Angle 180 sets the turtle to face the West.

Angle 270 sets the turtle to face the South.

Angle 360 or 0 set turtle to face the East

You can set the turtle to face any angle of your choice.

You can change the direction of the turtle using the following syntax

t.left(angle)

t.right(angle)

The above is used to rotate the turtle either to the left or to the right at the angle specified.

How to Change the Turtle Shape

The default shape of a turtle is known as a classic.

To change the shape of the turtle you use the shape() method.

The shape() method accepts a string as an argument. The string is the name of a valid shape in the TurtleScreen’s shape dictionary.

The valid turtle shapes are:

  • Arrow
  • Circle
  • Square
  • Triangle
  • Turtle
  • Classic

Like I said earlier, the default shape of the turtle is known as a classic.

I will show you my favorite turtle shape which you will love as well.

Open a new file editor window and type the following code snippet. Save it as turtle_shape.py

when you run the program above, you will get:

turtle shape of small shape size

Turtle Shape-“Turtle”

From the above screenshot, the turtle is small. To increase the size of the turtle, use the shapesize() method.

To illustrate this, run the following code snippet.

Here is the output of the above program.

turtle shape size

Turtle shape with a bigger shape size

How to Draw a Line

To draw a line use the syntax below

t.fd(distance)

or

t.goto(x,y)

distance is the value of the length of the line.

t.goto(x,y) moves the turtle to an absolute position

Note:

The Cartesian system is the coordinate system for Turtle. So x and y represent values in the x and y axis respectively.

t.fd(distance)  moves the turtle by the specified distance.

Open a new file editor window and type the following code snippet. Save it as draw_line.py

When you run the program above you will get:

draw a line

A line with a distance of 100 pixels

 

The above program moves the turtle 100 pixels to the east.

Open a new file editor window and type the following code snippet. Save it as turtle_go_to.py

Running turtle_go_to.py you would get,

drawing a line with the method goto()

Drawing a line with the method goto()

How to Increase the Thickness of a Line

t.pensize() accepts an integer as an argument to set the thickness of the line.

Open a new file editor and save the following code snippet as  turtle_line_thickness.py

from turtle import Turtle

when you run the code snippet above, you will get:

How to Hide the Turtle(Arrow Head)

You may not need the turtle to appear in your drawing as you just need a line. To achieve this open a new file editor and save the following code snippet as draw_line_without_turtle.py

Running this program will give you,

how to make the turtle disappear

How to make turtle disappear

You have learned a lot, so let us do something fun with Python.

Project 1:

Write a program to draw a square on the computer screen and save it as turtle_square.py

Let the background screen color be black and color of the line drawn red.

Solution 1: for a beginner that doesn’t know Python function and for loop

The above program first changes the window background to black and the turtle to red using t.screen.bgcolor(“black”) and  t.color(“red”).

The turtle is then made invisible using this line of code t.hideturtle()

The turtle moves 100 pixels to the east (t.fd(100)) and draws a horizontal line.

It then faces the north by rotating 90degrees(t.left(90)) to the left and moves 100 pixels to the north, drawing a vertical line.

t.left(90)  makes the turtle face the west by rotating it 90 degrees left. It then moves 100 pixels to the west, drawing a horizontal line.

t.left(90) makes the turtle face the south by rotating it 90 degrees left. It then moves 100 pixels to the south, drawing a vertical line.

The turtle turns 90 degrees left to face the east, it then moves 100 pixels to the east which is the default position of the turtle.

Finally, a square is drawn.

Solution 2:

Let’s make our code more readable and maintainable with function and a for loop. Save this as turtle_square1.py

Running turtle_square1.py, you will get a square on your computer screen.

drawing a square with turtle graphics

A square

Note:

Basically, the for loop runs through the code below four times.

 t.fd(length)

 t.left(90)

Did you ever do an isometric or auxiliary drawing?

Well, I did and to be honest with you, it was boring to me.

But Python made it fun as I could write some bunch of codes and my computer would do the drawing.

In the next project, you will draw an isometric block.

Project 2:

Write a program to draw a rectangle inclined to 30 degrees on the computer screen and save it as turtle_rectangle.py

Let the screen background color be black and color of the line drawn red.

Solution 1: for a beginner that doesn’t know Python function and for loop

Solution 2:

Open a new file editor window and type the following code snippet. Save it as turtle_rectangle1.py

Running program turtle_rectangle.py and turtle_rectangle2.py you would get

an inclined rectangle

An Inclined Rectangle

Can we do one more project?

Yes, of course.

Project 3:

Write a program to draw a triangle on the computer screen and save it as turtle_triangle.py

Let the screen background color be black and color of the line drawn red.

Solution:

Running the above program you would get

Don’t be over excited yet, there’s a ton of room for improvement in our programs.

How to Draw a Circle

The circle() method is used to draw a circle and it can accept three arguments which are:

  1. radius
  2. extent
  3. steps

The most important is radius and extent. Though, extent and steps are optional(i.e a circle can still be drawn without including them.)

The radius accepts an integer which is the radius of the circle.

The extent accepts an integer which is an angle. To cut short the technical explanation, it determines the part of the circle that will be drawn.

If you assign 360, a full circle will be drawn, 180 will draw a semi-circle. (Do you now understand why I said extent represents an angle?)

Let’s do some coding. The code snippet below will draw a semi-circle.

Open a new file editor window and type the following code snippet. Save it as turtle_semi_circle.py

Here’s the output of the code snippet.

How to draw a semi circle.

Semi circle

Well, let’s do something a bit harder so you can learn more.

Can you do something like this?

How to draw a semi circle

Semi Circle

If you could not do it, don’t worry.

Open a new file editor window and type the following code snippet. Save it as  semi_circle1.py

The only thing strange about this code snippet is t.setx().

The method setx() positions the turtle on the x-axis leaving the y-axis unchanged.

In addition, the method sety() positions the turtle on the y-axis leaving the x-axis unchanged.

In the next section, we will draw a full circle.

How to Raise Up the Turtle off the Drawing Surface

Imagine the turtle as a paint brush in the hand of an artist. The artist can lift up the brush off the canvas to place it on another position on the canvas.

To do a similar action with the turtle, you use this syntax below,

t.up()  

To put back the turtle on the canvas, use this syntax below,

t.down()

Note:

No drawing is done when the turtle is up.

Drawing is done when the turtle is down.

Let me explain the following syntax by writing cool programs

  • up()
  • down()
  • goto(x,y)
  • circle(radius)

I want you to observe the following code snippet and the diagram.

Code snippet 1:

Open a new file editor window and type the following code snippet. Save it as code_snippet_1.py

If you run the code above you will get;

drawing a circle with the circle() and goto() method

code snippet 1 screenshot

From the diagram above you can observe that the turtle moved to an absolute position (t.goto(100,50))

and a circle with a radius of 50 is drawn(t.circle(50))

The next code snippet will remove the line in the diagram above.

Code snippet 2:

Open a new file editor window and type the following code snippet. Save it as code_snippet_2.py

If you run the code above you will get;

draw circle with circle(), goto() and up() method

code snippet 2 screenshot

 

t.up() – raises the turtle up and no drawing is done when the turtle is up.

t.goto(100,50) – moves the turtle to an absolute position.

t.down() – brings down the turtle.

t.circle(50) – draws a circle with a radius of the value 50

You can replace t.up(), t.goto(100,50), t.down(), t.circle(50) with the method setposition()

The setposition() method accepts two arguments which are the coordinates x and y.

This method moves the turtle to an absolute position. If the turtle pen is down, a line will be drawn to the specified position.

How to Add colors in Your Drawing

We have been drawing many shapes but we have not applied colors to our drawing.

Here are some ways to apply colors to your drawing.

t.begin_fill() and t.end_fill() –

t.begin_fill() is called before drawing a shape to be filled with the color of the turtle.

t.end_fill() fills the shape drawn with the color of the turtle after t.begin_fill() has been called.

Open a new file editor window and type the following code snippet. Save it as red_circle.py

If you run the code above you will get:

drawing red cirlcle with begin_fill() and end_fill()

Red circle

t.fillcolor()

This meth0d changes the color of the turtle only, when the turtle moves, the color path remains the default color (black) .

The color can be passed as a string such as:

t.fillcolor(“blue”)

The next thing we are going to do will blow your mind.

Random Walk with Turtle

Open a new file editor window and type the following code snippet. Save it as turtle_random_walk.py

My output of the program is:

using the module random to draw in turtle

Random drawing

 Note:

We can’t get the same result.

Why?

This is because I use the random module.

A random module picks random element in a sequence.

How to Increase the Speed of the Turtle

Imagine how long it will take for your turtle to draw something like this:

Increasing the speed of turtle with speed() method

Random drawing

It would take you a lot of time.

But here’s the best part of turtle,

You can increase the speed of the turtle.

How then?

t.speed() accepts an integer as an argument to set the speed of the turtle. The value 0(zero) is the fastest speed of the turtle

So to increase the speed of the program (turtle_random_walk.py) include t.speed(0) before defining the function randomwalk.

How to draw a dot

The dot() method receives two optional arguments, which are:

  • The size of a dot
  • The color of the dot

Here is a simple dot of size 20 and blue in color.

drawing a dot with python turtle using the dot() method

A dot

Here is the code snippet.

So we have been drawing shapes on our computer screen, and am getting really bored of it.

Let’s do something a bit different by writing text on the computer screen.

How to Write Text with Python Turtle

To write on your computer screen, use the write() method.

The write() method accepts four arguments, which are:

  • arg– this is the string object of the text you want to write to the TurtleScreen.
  • move– this argument is optional and it can only be assigned to the value “True” or “False”. When True a line will be drawn below the text.
  • align– you assign this argument either to the left, right or center. This argument is also optional
  • font– this argument basically determines the font name, font size, font type of your text.

To understand this better, let’s write some codes.

Project 4:

Write a program to display Cool Python Codes on the turtle screen.

Open a new file editor and save the following code snippet as write_program.py

Here is the output of the program.

displaying text on turtle screen

Cool Python Codes

The cool font I used is “Freestyle Script” and I got it from Microsoft word application.

Let’s do something fun in Python.

I want to do some logo designs for my blog, and I don’t want to use Photoshop.

Guess what?

I will do it in Python.

Project 5: How to do logo design with Python

Here are some code snippets to the logo design for my blog.

Logo design 1:

Open a new file editor and save the following code snippet as Cool_Python_Codes_Logo1.py

Here is the output of the program above.

logo python turtle

Code Python Codes logo design 1.

Logo design 2:

Open a new file editor and save the following code snippet as Cool_Python_Codes_Logo_2.py

Here is the output of the program above.

designing logo with python turtle

Cool Python Codes logo design 2

Logo design 3:

Open a new file editor and save the following code snippet as Cool_Python_Codes_Logo_3.py

Here is the output of the program above.

logo design using python turtle

Cool Python Codes logo design 3

The logo below was designed using Photoshop by Tutorial Graphics.

Cool Python Codes logo

Cool Python Codes logo

Can this tutorial get more exciting?

Yes, it can.

Let’s write programs to respond to user’s mouse clicks.

onclick() method

This method calls a function whenever the mouse is used to click on a particular coordinate.

It accepts three arguments which are:

  • fun– this is a function with two arguments, to which will be assigned the coordinates(x,y) of the clicked point on the canvas.
  • btn– this argument is assigned to one by default, which means you click the left mouse button to call a function. If you assign it to two, you will have to click both the left and right mouse button simultaneously to call a function.
  • add– True or False. If True, a new binding will be added, otherwise, it will replace a former binding.

I usually use one argument which is the function I want to call when I click my mouse.

Here is a program that draws a circle when you click on the turtle. Save the code snippet as click_to_draw_circle.py

Note:

The function name “circle” has two arguments, x and y which are the coordinates you will have to click to draw a circle.

ondrag() method

This method is basically used when you what to click on the turtle and move it. It accepts three arguments like the onclick() method.

Here is a program to illustrate the ondrag() method.

The code snippet below is saved as turtle_ondrag.py.  

Here’s what I drew using the program above.

drawing with you mouse in python turtle using the method ondrag()

I drew a cup.

onrelease() method

This method is also similar to onclick and ondrag method. To understand what this method does,

I wrote a program to draw a circle when you left click on the turtle and when you remove your hand from the mouse, the circle will disappear.

Here is the code snippet and it is saved as turtle_onrelease.py

Note:

You have to leave your hand on the left-hand button of the mouse until the circle is drawn on the screen.

When the circle has been drawn, you can then remove your hand from your mouse. Once you remove your hand from the mouse, the circle disappears.

This circle disappears because of the clear()  method. The clear() method deletes the turtle’s drawing on the screen.

How to control the turtle with your keyboard

This section is going to be fun as we will control the turtle with the keyboard of our computer.

Before we write some codes, let me explain the following concepts.

  1. onkey()
  2. listen()

onkey()

This method is similar to onclick() method. The only differences is that the onclick() method responds to clicks on the mouse while the onkey() method responds to commands from your computer keyboard.

The onkey() method accepts two arguments which are:

  • fun: this is a function without any argument
  • keyboard command: this is basically any alphabet on your keyboard (e.g n) or any keyboard command key. e.g space, your direction keys(up, down). You have to pass this command as a string

listen()

I will avoid using any technical word because I’m as well bored with them. Basically, without including this method, the turtle won’t obey your commands.

The listen() method is like a cane you use on a child to obey your instruction.

Alright, let’s write some codes.

Project 6:

Write a program to control the turtle to move up, down, left and right using the computer keyboard. The turtle should move constantly to any pixels of your choice.

Include an undo action.

Open a new file editor and save the following code snippet as turtle_keyboard_control.py

Don’t let the long code snippet to confuse you.

I will quicky explain the program above.

I mentioned early that :

Angle 90 sets the turtle to face the North.

Angle 180 sets the turtle to face the West.

Angle 270 sets the turtle to face the South.

Angle 360 or 0 set turtle to face the East

Alright!!!…

Now the “if”, “ else “ and “not” tells the turtle that if it’s not facing the desired direction, that it should change the direction.

t.heading() returns the  current turtle’s current heading.

t.undo() undo the last action of the turtle.

The key commands are “Up”, “Down”, “Left”, “Right” and “End” which are your direction keys and end key on the keyboard. Note that they all start with capital letters.

The rest of the code snippets are easy to understand.

stamp() method

This method basically pastes the turtle shape on the canvas on the current turtle’s position.

To illustrate this, open a new file editor and save the following code snippet as turtle_stamp.py

 

Here’s the output of the above code snippet.

Observing the above screenshot, you will see that the turtle shape appeared 4 times. This makes the concept of for loop more practical. For every 50pixels to the east, a turtle shape will appear.

How to Determine the Position of the Turtle.

position() method:

This method returns the position of the turtle, both the coordinates of the x and y axis is returned.

xcor() method:

This method returns the x coordinate of the turtle.

ycor() method:

This method returns the y coordinate of the turtle.

One more last thing before I end this tutorial.

How to Change the Title of the Turtle Screen

The default title of the turtle screen is “Python Turtle Graphics”, which can be boring.

It will be fun if your name or your friend’s name is on it. Your friends will know that you’re a Pro programmer.

To change the title of the turtle screen, use the method screen.title(). This method only accepts a string.

To illustrate this method, open a new file editor and save the following code snippet as turtle_screen_title.py

Here is the output of the program above.

How to change the title of Python turtle.

How to change the title of Python turtle.

Here are some amazing drawings I did with Python Turtle including the code snippet.

Try and study the code snippet and do a lot of experiments to come up with new designs.

The below code snippet is saved as beautiful_circles.py

Here’s the output of the above code snippet.

beautiful circles with Python turtle - Python Turtle Graphics - The Complete Guide.

Beautiful circles with Python Turtle

The below code snippet is saved as beautiful_square.py

Here’s the output of the code snippet above.

beautiful square with python turtle - Python Turtle Graphics - The Complete Guide.

Beautiful Square

The below code snippet is saved as beautiful_spiral.py

Here’s the output of the above code snippet.

new - Python Turtle Graphics - The Complete Guide.

beautiful spiral

You have come to the end of this tutorial.

Please don’t forget to:

Please if you have any questions or contributions, please leave it in the comment section.

Thanks for reading. If you enjoyed this tutorial, hit the share button. It would mean a lot to me and it helps other people to see the tutorial.

It would mean a lot to me and it helps other people to see the tutorial.

About the author

Rapture Godson

I am Godson; the brain behind Cool Python Codes. On this blog, I will make Python much fun and very practical. My up-to-date tutorials are based on my studies and they are very easy for you to understand.

23 Comments

  • I am a newbie of python. I got confused even from the first line: “from turtle import Turtle”.
    According to what I learned from tutorial, it means “to import Turtle.py from turtle (folder / package)”. But I can’t find any Turtle.py in the source which I downloaded. The more weird is that it works. Could you tell me why?

        • I made research about your question.

          Here’s the answer I got from StackOverFlow.

          Python’s turtle.py is unusual in that it presents both a function-based interface and an object-oriented interface. Depending on how you import it, you can work with one, or the other, or both.

          Here, we are using the object-oriented interface to invoke the screen method bgcolor():

          from turtle import Turtle

          t = Turtle()
          t.screen.bgcolor(“black”)

          I usually write this as:

          from turtle import Turtle, Screen

          screen = Screen()
          screen.bgcolor(“black”)

          t = Turtle()

          as having direct access to the Screen object simplifies things. Using this style import, you cannot access the function-based interface.

          When we do this simpler import, we have access to both the function-based interface and the object-oriented interface. Here, we’re using the function bgcolor() to set the background color:

          import turtle

          turtle.bgcolor(“black”)

          Using either the function-based or object-oriented interface to turtle.py is fine, but you can get yourself seriously confused when mixing the two.

          I’m so happy that you asked this question because I have learned a lot from it also.

          I hope you are satisfied now.

  • I imported ‘beautiful_square’ and got correct result. Then, I did ‘beautiful_circles’ and it & python interpreter crashed. I restarted the python interpreter and imported ‘beautiful_circles’ and got correct result. But it crashed again once I imported ‘beautiful_square’ then.
    How come? The two module can’t be imported at the same time. I even found similar phenomenon at other modules.

  • I imported ‘turtle_background_pic.py’ and it crashed without showing the owl. My python version is 3.6. Could you help me?

    • I expect you run the program and not to import it.

      Also take note of the following:

        Only the gif image format is accepted.
        Let your program and the gif image be in the same folder on your computer.
        Provide the right name of the gif image.
      • Importing a module doesn’t cause it been run? How can I run it except importing it. I am a newbie. I always run a module by importing it.
        That program and gif image were downloaded from here. Yes, they are at the same folder. Image name is right too.

        • If you right click on the program and click on edit with IDLE.

          Once you are in the Python IDLE, click Run>Run Module or press f5.

          This is how to run the program.

          note am using windows.

          • I’m using Python Interpreter at Windows. Importing command is the only way I know from tutorial to run a module at python file at interpreter. Surely, I can run invoke python interpreter to run that module from command prompt too. Any other way to run a module from python interpreter except importing it?

Leave a Comment