Python Turtle Module – A Complete Guide For Creating Graphics In Python

Hey python geeks, here is a very interesting tutorial for you, so welcome to Python Turtle Module tutorial. In this tutorial, you will learn about turtle module in python, how to create graphics in python using turtle and many more. I am gonna sure, you will really enjoy this awesome tutorial.  So lets start the tutorial without wasting time.

Graphics designing is so much funny and everyone enjoy this. You can also design graphics in python. Are you thinking, How ? – then follow this tutorial till end.

As we all know, Python is most popular language for current time. You can do any thing with python. So if you are thinking about what can you do with python then read this article python applications, that will give you a brief information of the areas where you can use python.

Python Turtle Module – Design Your imagination With Turtle

Give a look on below picture, that is looking really awesome and attractive. Can you imagine, you can design these graphics in python. Yes you can design these types of graphics and many others in python too and that is possible because of Turtle module.

Python Turtle Module
Python Turtle Module

So now question is that what is turtle. And now i am going to explain everything about turtle, so stay with patience till the end, because this tutorial is going to be long.

What Exactly Is Turtle ?

  • Turtle is a built in module in python.
  • It was part of the original Logo programming language developed by Wally Feurzig and Seymour Papert in 1966.
  • Using turtle you can draw any shape, image on the screen and it is fun to work with turtle graphics.
  • The great use for turtle is teaching kids basic programming. The young ones are fascinated by visually appealing designs rather than text.
  • Turtle is a toolkit that provides a simple and enjoyable way to draw pictures on windows or screen.
  • We can say that, turtle graphics controlling a graphical entity in a graphic window with x,y coordinates. 

Python Turtle Module Tutorial – Setting Turtle Screen

In this tutorial, I will make Python more practical for you and also fun. So I will do my best to make every bit of this tutorial exciting. So let’s start.

Creating A Turtle Window

First of all we will learn how to create a turtle window. So for this write the following code.

Explanation

  • First of all you have to import turtle module.
  • The next thing is to initialize a turtle object from Turtle() class. You can give any name to turtle object.
  • When you will 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 turtle.done() in your program. It should be the last statement of your program.
  • And now let’s check how the turtle window looks like. So run this code.
Python Turtle Module
Python Turtle Module
  • In the turtle window, you can see a arrow head at the centre of the window and it is pointing towards the right. This is the default position.
  • (0,0) is the default position of a turtle which is centre of window.

Changing Turtle Window Background Color

  • The default window color is white, which may be boring for you or may be some time you need some attractive background for your application.
  • Let’s see how to change turtle window background color.

  • For changing window background color, you have to use screen.bgcolor() method
  • The screen.bgcolor() method accepts one argument which is the color of the turtle window and it should be string.

And now you will get an attractive window, amazing.

Python Turtle Module
Python Turtle Module

Setting Turtle Window Background Image

You can also set background image in turtle window. For this write the following code.

  • bgpic() method is used to set the window background image.
  • It takes an argument that is image name and must provide the format of image.
  • The image must be placed in the same folder where your python file is placed.

So let’s see the result –

Changing Turtle Title

Python Turtle Graphics is default title of turtle, but you can also change turtle title according to your choice. So write the following code for changing title.

And the result is –

Python Turtle Module
Python Turtle Module

Python Turtle Module Tutorial – Customizing Turtle

Changing Turtle Color

For changing color of turtle, you have to use color() method of turtle module.

  • color() method takes color name as argument.
  • So write the following code.

Now see the result –

Python Turtle Module
Python Turtle Module

Changing Turtle Shape

Now you are thinking that, a turtle is to be created but there is a arrow, what’s the logic for this. So the best thing is that even you can change this shape. So write the following code.

  • For specifying a particular shape of turtle, what you need is to specify a turtle shape into shape().
  • Let’s see the result.
Python Turtle Module
Python Turtle Module

Finding Available Shape Of The Turtle

If you want to know that which type of shape can you draw then run the following command for checking supported shapes by turtle module.

On running this code you will get following result –

Python Turtle Module
Python Turtle Module

Increasing Turtle Size

You can also increase the size of turtle. shapesize() method is used to increase the turtle’s size. It takes an argument in integer form. So write the following code.

Result

Python Turtle Module
Python Turtle Module

Hiding The Turtle

Now if you want to hide the turtle, you can do so.

  • hideturtle() method is used to hide the turtle.
  • So write the following code for implementing it.

Result

Python Turtle Module
Python Turtle Module

You can see, there is no turtle because it is hidden.

Python Turtle Module Tutorial – Turtle Movement

In this section we will discuss about turtle motions. There is nothing but how to move turtle in different directions. By default turtle is placed in (0,0) and this is called home position.

Changing Turtle Movement

You can move turtle either in forward or backward direction.

Forward Movement

If you want to move turtle forward, you have to use forward() method.

  • forward() method moves the turtle forward by the specified distance, in the direction
    the turtle is headed.
  • It takes an argument i.e., distance, that must be integer or float.
  • forward() and fd() are same, you can use any one.

So to move turtle forward, write the following code –

Let’s see the result –

Python Turtle Module
Python Turtle Module

Backward Movement

If you want to move turtle backward, you have to use backward() method.

  • backward() method moves the turtle backward by distance ,opposite to the direction the
    turtle is headed. Do not change the turtle’s heading.
  • It takes an argument i.e., distance, that is number.
  • backward(), bk() and back() are same, you can use any one.

So to move turtle backward, write the following code –

Python Turtle Module
Python Turtle Module

Changing Turtle Direction

By default, turtle faces towards east but you can also change the direction of heading of turtle.

  • setheading(“angle”) method is used to set the head of turtle.
  • This method takes angle as an argument.

Let’s see this with an example, so write the following code –

Here i have created four turtles for showing heading. Meaning of angles is following :

Angle 90 = East

And Angle 180 = West

Angle 270 = South

Angle 360 = North

And the result of this code is following –

Python Turtle Module
Python Turtle Module

In the result, You can see that Yellow turtle is facing towards West direction, Red turtle is facing towards North direction, Blue turtle is facing towards East direction and Black turtle is facing towards South direction.

Rotating Turtle

Now we will rotate our turtle. It can be rotated either to the left or to the right at the angle specified.

Left Rotation

  • left(angle) function turn turtle left by angle units.
  • Units are by default degrees, but can be set via the degrees() and radians() functions.
  • left() and lt() are same.

So write the following code for rotating turtle to the left.

Result

Python Turtle Module
Python Turtle Module

Right Rotation

  • right(angle) function turn turtle right by angle units.
  • Units are by default degrees, but can be set via the degrees() and radians() functions.
  • right() and rt() are same.

So write the following code for rotating turtle to the right.

Result

Python Turtle Module
Python Turtle Module

Python Turtle Module Tutorial – Design A Fractal Tree Using Turtle

Now you will learn to design a most amazing graphic that is fractal tree. It is very attractive and beautiful. So let’s design it in python using turtle module.

First of all you have to understand, what is fractal tree – A binary fractal tree is defined recursively by symmetric binary branching.

The trunk of length 1 splits into two branches of length r, each making an angle q with the direction of the trunk. Both of these branches divides into two branches of length r2, each making an angle q with the direction of its parent branch.

Continuing in this way for infinitely many branchings, the tree is the set of branches, together with their limit points, called branch tips.

Python Turtle Module
Python Turtle Module

This is our fractal tree that is looking awesome and we have to design this using turtle. I am designing this using recursion. Learn more about Recursion.

Let’s write the following code –

Let’s see the process of designing fractal tree in python.

Now, if you want to create fractal with multicolor then add some lines into the previous code.

And now your fractal tree is more attractive and beautiful. Let’s see  –

Python Turtle Module
Python Turtle Module

So guys i hope, you enjoyed creating fractal tree in python. 

Related Articles :

So this was all about Python Turtle Module tutorial. I hope it will be very helpful for you. And you can share your experience of designing turtle graphics with me. If you have any doubt regarding this post then leave your comment in comment section. In the next tutorial, i will teach you to design some special and attractive turtle graphics designs, till then stay tuned with Simplified Python. Thank You

4 thoughts on “Python Turtle Module – A Complete Guide For Creating Graphics In Python”

Leave a Comment