How to draw a shape in python using Turtle (Turtle programming in Python)

Want to know more about Python Turtle? In this Python tutorial, we will discuss turtle programming in python and, we will see what is Python Turtle and how to use it in Python. Also, We will see the below topics as:

  • What is Turtle in python?
  • How to install turtle in python
  • Python turtle methods
  • Python turtle speed
  • Python turtle speed fastest
  • Change turtle size python
  • Python turtle change pen size
  • Change turtle shape python
  • Python turtle screen size
  • Python turtle how to set position
  • Turtle onscreen click example python
  • How to get coordinate of the screen in python turtle
  • Change the screen title in python turtle
  • Python turtle clear screen
  • How to draw a square in python using turtle
  • How to draw a rectangle in python using turtle
  • How to draw a circle in python using turtle
  • How to draw ellipse in python using turtle
  • Code to draw a star in python turtle
  • Draw a pentagon in python using turtle
  • Draw a hexagon in python turtle
  • Python program to draw Heptagon using turtle
  • Draw octagon in python using turtle
  • Draw a polygon in python using turtle
  • Draw a dot in python using turtle
  • Python draw tangent circles using turtle
  • Python draw spiral circles using turtle
  • Python draw concentric circles using turtle
  • How to draw a spiral square in python turtle
  • Draw spiral star in python turtle
  • Draw a spiral triangle in python turtle
  • Draw cube in python using turtle
  • How to draw a grid in turtle python
  • Python turtle graphics not responding
  • Python turtle mainloop
  • How to activate check if button is pressed on python turtle
Table of Contents show

What is Turtle in python?

  • “Turtle” is a python feature like a drawing board, which allows you to command a turtle to draw all over it.
  • We can use the function like turtle.forward(….) and turtle.left(….) which will move the turtle around.
  • To use a turtle, we have to import it first.
  • Just go to the python environment and type “import turtle”.
  • The python turtle library contains all the methods and functions that we need to create an image.

You may also like Python Tkinter Stopwatch.

How to install turtle in python

To install turtle in python, we have to run the below command in terminal:

$ pip install turtle

Python turtle methods

Some of the mostly used methods are:

  • forward() – It moves the turtle forward by the specified amount
  • backward() – It moves the turtle backward by the specified amount
  • right() – It turns the turtle clockwise direction
  • left() – It turns the turtle anticlockwise direction
  • penup() – It stops drawing of the turtle pen
  • pendown() – It starts drawing of the turtle pen
  • color() – It changes the color of the turtle pen

Python turtle speed

  • The turtle.speed() method is used to change the speed of the turtle. We can pass the value of the argument that it takes. It will return or set the speed of the turtle.
  • The speed lies in the range of 0-10.
  • The speed values are in the following ways:
    • fastest – 0
    • fast – 10
    • normal – 6
    • slow – 3
    • slowest – 1
  • The syntax used for the speed of turtle “turtle.speed(number)”

Example:

import turtle 
turtle.speed(1)
turtle.forward(100)
turtle.done()

In this output, we can see that the new window appears and the turtle speed is “1” which is the slowest and it is moving forward by 100 units.

Python turtle speed
Python turtle speed

Python turtle speed fastest

The turtle.speed() method is used to change the speed of the turtle. We can pass the value of the argument that it takes. Here, the speed is “0” which is the fastest.

Example:

import turtle 
turtle.speed(0)
turtle.forward(200)
turtle.done()

In this output, we can see that the new window appears and the turtle speed is “0” which is the fastest and it is moving forward by 200 units.

Python turtle speed fastest
Python turtle speed fastest

Change turtle size python

Here, we will see how we can change the turtle size in python

  • To change the size of the turtle, we can increase or decrease the size of a turtle to make it bigger or smaller. This will only change the turtle size without affecting the output of the pen as it draws on the screen.
  • The syntax used for changing the size of a turtle is “turtle.shapesize(stretch_width, stretch_length, outline)”.

Example:

import turtle 
tr = turtle.Turtle()
tr.shapesize(10,5,1)
turtle.done()

In this output, we can see that we have used the “tr.shapesize(10,5,1)” for changing the size of the turtle and you can change size according to your preference. The new window will appear and you can see the turtle size is changed.

Change turtle size python
Change turtle size python

Python turtle change pen size

To increase or decrease the thickness of pen size we can use the “tr.pensize()”. Now you can see that the size of the pen is 4 times the original size.

Example:

import turtle
tr = turtle.Turtle()
tr.pensize(4)
tr.forward(200)
turtle.done()

In this output, we can see that the new window appears and the pen size is changed. The turtle will move forward by 200 units.

Python turtle change pen size
Python turtle change pen size

Change turtle shape python

As we know the initial shape of a turtle is not really a turtle, but a triangle shape. However, if you want to change the look of a turtle to any other shape like circle, square, arrow, etc then you can use “tr.shape(“square”)”.

Example:

import turtle 
tr = turtle.Turtle()
tr.shape("square")
turtle.done()

In this output, we can see that the new window appears and the shape of the turtle is changed to a square.

Change turtle shape python
Change turtle shape python

How to move turtle with mouse in python turtle

  • First, we need to “import turtle”. The function with two arguments, x and y will be assigned, the coordinates of the checked point on the canvas.
  • The turtle.ondrag() is used to move the mouse on this turtle on canvas.
  • The turtle.setheading(turtle.towards(x, y)) is used to move the turtle’s angle and direction towards x and y.
  • Here, turtle.ondrag(func) is called again. To set turtle speed we have used “turtle.speed(10)”.
  • The sc.setup(600, 600) is used to set the screen, and sc.mainloop() is used to take the screen in the mainloop.

Example:

import turtle
def func(x, y):
    turtle.ondrag(None)
    turtle.setheading(turtle.towards(x, y))
    turtle.goto(x, y)
    turtle.ondrag(func)
turtle.speed(10)
sc = turtle.Screen()
sc.setup(600, 600)
turtle.ondrag(func)
sc.mainloop()

In this output, we can see how to move the turtle with the mouse in the new window.

How to move turtle with mouse in python turtle
How to move turtle with mouse in python turtle

Python turtle screen size

The “turtle.screensize()” method is used to resize the canvas the turtle is drawing on. If no argument is given, the function returns the current (canvwidth, canvheight). Here I have taken (canvwidth=600, canvheight=600, bg=”blue”).

Example:

import turtle 
turtle.screensize(canvwidth=600, canvheight=600, bg="blue") 
turtle.done()

In this output, we can see that the new window appears and the turtle screen size is changed and the color of the screen is blue.

Python turtle screen size
Python turtle screen size

Python turtle how to set position

Let’s see how to set position using turtle in Python

  • Firstly, we will import turtle module. To create a screen object we will use “tr = turtle.Screen()”
  • To change the color of the screen at any time, we can use the command “turtle.bgcolor(*args)”.
  • The tr.setup(width=500, height=450, startx=0, starty=0) is used to set the size and position. We can change or set position by changing the startx and starty.
  • Here, my startx and starty are set to “0”. So the position will be at the top of the screen.

Example:

import turtle
tr = turtle.Screen()
tr.bgcolor('grey')
tr.setup(width=500, height=450, startx=0, starty=0)
turtle.done()

In this output, we can see that the new window appears, and the position is set on the turtle graphics.

Python turtle how to set position
Python turtle how to set position

Turtle onscreen click example python

  • Firstly, we will import the package turtle and random. And then we will make a list of colors.
  • Now, we will define the method to call on the screen click. We have to set screen color randomly by using “scr.bgcolor(color[r])”.
  • We have already seen that by default turtle always opens up the screen with a white background and using “turtle.onscreenclick(fun)” will bind the function to a mouse-click event.
  • The above method is used to set the background with changing colors on clicking the turtle screen.

Example:

import turtle 
import random 
color = ['purple', 'pink', 'red', 'yellow', 'green', 
       'black', 'blue', 'orange',] 
def fun(x, y): 
    global color
    r = random.randint(0, 7) 
    scr.bgcolor(color[r]) 
scr = turtle.Screen() 
scr.setup(500, 400) 
turtle.onscreenclick(fun)
turtle.done()

In this output, we can see that the new window appears, and whenever the user will click on the screen it changes the background color of the turtle graphic window randomly.

Turtle onscreen click example python
Turtle onscreen click example python
Turtle onscreen click example python
Turtle onscreen click example python

How to get coordinate of the screen in python turtle

  • Firstly, we will import turtle module. The turtle () method is used to make objects.
  • The turtle.onscreenclick(buttonclick,1) is used to send the coordinate to function and 1 is used for left click.
  • Here, speed is used to increase or decrease the speed of the turtle.
  • The listen() allows the server to listen to incoming connections.

Example:

import turtle
tr = turtle.Turtle()
def buttonclick(x,y):
    print("Clicked at the coordinate({0},{1})".format(x,y))
turtle.onscreenclick(buttonclick,1)
turtle.listen()
turtle.speed(1)
turtle.done()

In the below output, we can see that the new window appears and by clicking anywhere on the screen, we get the coordinate of the screen on a terminal.

How to get coordinate of the screen in python turtle
How to get coordinate of the screen in python turtle

Change the screen title in python turtle

The turtle.title() function is used to set the title of a turtle window. It requires only one argument as a string which will appear in the titlebar of the turtle graphics window. By default title of the turtle graphics window is “Python Turtle Graphics”.

Example:

import turtle
turtle.title("My turtle window")
turtle.done()

In this output, we can see that the new window appears and the screen title is changed to “My turtle window”.

Change the screen title in python turtle
Change the screen title in python turtle

Python turtle clear screen

The turtle.clear() function is used to delete the turtle drawings from the screen. It does not require any argument.

Example:

import turtle 
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.clear()
turtle.done()

In the below output, we can see that the new window appears.

Python turtle clear screen
Python turtle clear screen

How to draw a square in python using turtle

Let us discuss, how to draw a square in python using turtle.

  • First, we need to “import turtle”.
  • After importing we have all the turtle functionality available.
  • We need to create a new drawing board and a turtle.
  • Let’s call turtle as “tr”
  • To move the turtle forward we have used “tr.forward()” and to move the turtle in right we used the “tr.right() method which will rotate in place.
  • turtle.done() tells the compiler that the program ends.

Example:

import turtle
tr = turtle.Turtle()
for i in range(4):
    tr.forward(60)
    tr.right(90)
turtle.done()

In this output, we can see that the new window appears and the turtle as tr is moving forward and right by using the for loop.

How to draw a square in python using turtle
How to draw a square in python using turtle

How to draw a rectangle in python using turtle

Now, we will see how to draw a rectangle in python using turtle.

  • Here, we have imported the turtle module. Now, let’s call turtle as “tr”
  • The for loop is used to print the code number of times.
  • To move the turtle forward we have used “tr.forward(300)” and it moves 300 pixels in the direction it is facing and to move the turtle in right we used the “tr.right(90) method which will rotate in place 90 degrees clockwise.
  • turtle.done() tells the compiler that the program ends.

Example:

import turtle 
tr = turtle.Turtle()
for i in range(2):
    tr.forward(300)
    tr.right(90)
    tr.forward(150)
    tr.right(90)
turtle.done()

In this output, we can see the rectangle is drawn in the new window. The turtle tr is moving forward(300) pixels in the direction tr is facing and tr.right(90) it rotates in places 90 degrees clockwise.

How to draw a rectangle in python using turtle
How to draw a rectangle in python using turtle

How to draw a circle in python using turtle

Let’s draw a circle in python using turtle.

  • To draw a circle, we have to use the module called import turtle, and then we will use the circle() method.
  • The circle method takes radius as an argument.

Example:

import turtle 
tr = turtle.Turtle()
rad = 80
tr.circle(rad)
turtle.done()

In this output, we can see the circle is drawn on the new drawing board. It draws the circle of radius of 80 units. You can refer to the below screenshot.

How to draw a circle in python using turtle
How to draw a circle in python using turtle

How to draw ellipse in python using turtle

Let’s draw ellipse in python using turtle.

  • To draw an ellipse, we have to use the module called import turtle, and then we will define a function.
  • The for loop is used to draw an ellipse. Divide the ellipse and tilt the shape to negative “45”.
  • Call the draw method at the end.

Example:

import turtle
def draw(rad):
  for i in range(2):
    turtle.circle(rad,90)
    turtle.circle(rad//2,90)
turtle.seth(-45)
draw(150)
turtle.done()

In this output, we can see the ellipse is drawn on the new drawing board. You can refer to the below screenshot.

How to draw ellipse in python using turtle
How to draw ellipse in python using turtle

How to draw a star in Python turtle

Let us see how to draw a star in Python using Turtle.

  • To draw a star, we have to use the module called import turtle, and then we will use the for loop to print the code number of times.
  • Here, the turtle will move forward by 80 units and then it turns towards the right by 144 degrees.

Example:

import turtle 
tr = turtle.Turtle()
for i in range(5):
    tr.forward(80)
    tr.right(144)
turtle.done()

In this output, we can see that the star is drawn on the new drawing board. Also, we have to remember that we have to turn the turtle by 144 degrees only. If you turn it to other angles then you will not be able to draw the star.

Code to draw a star in python turtle
Code to draw a star in python turtle

Draw a pentagon in Python using turtle

Let us discuss, how to draw a pentagon in Python using turtle.

  • To draw a pentagon in python using turtle, we have to use the module called import turtle, and then we will use the for loop to print the code number of times.
  • Here, the turtle will move forward by 80 units and then it turns towards the right by 72 degrees clockwise.
  • An exterior angle of a polygon is 360/(number of sides). So, for the pentagon, it will be 72.

Example:

import turtle 
tr = turtle.Turtle()
for i in range(5):
    tr.forward(80)
    tr.right(72)
turtle.done()

In this output, we can see that the pentagon is drawn on the new drawing board. The exterior angle of a pentagon is 72 degrees and the statement are repeated 5 times to obtain a pentagon.

Draw a pentagon in python using turtle
Draw a pentagon in python using turtle

Draw a hexagon in Python turtle

Let us see how to draw a hexagon in Python turtle.

  • To draw a hexagon in python using turtle, we have to use the module called import turtle, and then we will use the for loop to print the code number of times.
  • Here, the turtle will move forward by 100 units assuming the side of hexagon and then it turns towards the right by 60 degrees clockwise.
  • An exterior angle of a polygon is 360/(number of sides). So, for the hexagon , it will be 60.

Example:

import turtle 
tr = turtle.Turtle()
for i in range(6):
    tr.forward(100)
    tr.right(60)
turtle.done()

In this output, we can see that the hexagon is drawn on the new drawing board. The exterior angle of a hexagon is 60 degrees and the statement are repeated 6 times to obtain a hexagon.

Draw a hexagon in python turtle
Draw a hexagon in python turtle

Python program to draw Heptagon using turtle

Let us see how to draw Heptagon using turtle in Python?

  • To draw a heptagon in python using turtle, we have to use the module called import turtle, and then we will use the for loop to print the code number of times.
  • Here, the turtle will move forward by 100 units assuming the side of heptagon, and then it turns towards the right by 51.42 degrees clockwise.
  • An exterior angle of a polygon is 360/(number of sides). So, for the heptagon , it will be 51.42.

Example:

import turtle 
tr = turtle.Turtle()
for i in range(7):
    tr.forward(100)
    tr.right(51.42)
turtle.done()

In this output, we can see that the heptagon is drawn on the new drawing board. The exterior angle of a heptagon is 51.42 degrees and the statement is repeated 7 times to obtain a heptagon.

Python program to draw Heptagon using turtle
Python program to draw Heptagon using turtle

Draw octagon in Python using turtle

Here, we will see how to draw octagon in Python using turtle?

  • To draw a octagon in python using turtle, we have to use the module called import turtle, and then we will use the for loop to print the code number of times.
  • Here, the turtle will move forward by 100 units assuming the side of octagon and then it turns towards the right by 45 degrees clockwise.
  • An exterior angle of a polygon is 360/(number of sides). So, for the octagon , it will be 45.

Example:

import turtle 
tr = turtle.Turtle()
for i in range(8):
    tr.forward(100)
    tr.right(45)
turtle.done()

In this output, we can see that the octagon is drawn on the new drawing board. The exterior angle of an octagon is 45 degrees and the statement are repeated 8 times to obtain an octagon.

Draw octagon in python using turtle
Draw octagon in python using turtle

Draw a polygon in python using turtle

Let’s see how to draw a polygon in python using turtle?

  • To draw a polygon in python using turtle, we have to use the module called import turtle, and then we will use the for loop to print the code number of times.
  • Here, the turtle will move forward by 100 units assuming the side of the polygon and then it turns towards the right by 40 degrees clockwise.
  • An exterior angle of a polygon is 360/(number of sides). So, for the polygon, it will be 40.

Example:

import turtle 
tr = turtle.Turtle()
for i in range(9):
    tr.forward(100)
    tr.right(40)
turtle.done()

In this output, we can see that the polygon is drawn on the new drawing board. The exterior angle of an polygon is 40 degrees and the statement are repeated 9 times to obtain a polygon.

Draw a polygon in python using turtle
Draw a polygon in python using turtle

Draw a dot in python using turtle

Let’s how to draw a dot in Python using turtle?

  • To draw a dot, we have to use the module called import turtle, and then we will use the dot() method.
  • The number within the bracket is the diameter of the dot.
  • We can increase or decrease the size by changing the value of the diameter, here d=30.

Example:

import turtle 
tr = turtle.Turtle()
d = 30
tr.dot(d)
turtle.done()

In this output, we can see the dot is drawn on the new drawing board. It draw a dot which is filled in circle and the size of dot can be changed by changing the diameter. You can refer to the below screenshot.

Draw a dot in python using turtle
Draw a dot in python using turtle

Python draw tangent circles using turtle

Let’s see how to draw a tangent circles using turtle in Python?

  • A tangent circles will have more than one circle having one point of intersection is called tangent.
  • To draw a tangent circles, we have to use the module called import turtle, and then we will use the circle() method.
  • Here, for loop is used for printing the tangent circle. This loop will start from 0 and will repeat till the given range-1.

Example:

import turtle 
tr = turtle.Turtle()
for i in range(12):
    tr.circle(12*i)
turtle.done()

In this output, we can see the tangent circles is drawn on the new drawing board. The turtle reaches the same point from where it has started drawing the circle. This circle will repeat one less then the given range to obtain tangent circles.

Python draw tangent circles using turtle
Python draw tangent circles using turtle

Python draw spiral circles using turtle

Now, we will see how to draw spiral circles using turtle in Python?

  • A spiral circles with varying radius are called spiral.
  • To draw a spiral circles, we have to use the module called import turtle, and then we will use the circle() method.
  • Here, the initial radius is 10 and for loop is used for spiral circle. This loop will start from 0 and will repeat till the given range.
  • Also, we have passed 45 as a central angle, and it will be repeated 100 times to obtain spiral circles.

Example:

import turtle 
tr = turtle.Turtle()
r = 10
for i in range(100):
    tr.circle(r+i, 45)
turtle.done()

In this output, we can see the spiral circles is drawn on the new drawing board and it is repeated 100 times to obtain spiral circle.

Python draw spiral circles using turtle
Python draw spiral circles using turtle

Python draw concentric circles using turtle

Let’s see how to draw a concentric circles using turtle in Python?

  • A circles with different radius having a common center are called concentric circles.
  • To draw a concentric circles, we have to use the module called import turtle, and then we will use the circle() method.
  • The for loop is used for printing the concentric circles.
  • Here, we have picked up the turtle pen and set the y coordinate of turtle pen to -1 times 10*i. After that we have put down the pen.
  • This will be repeated 30 times to obtain concentric circles.

Example:

import turtle 
tr = turtle.Turtle()
r = 10
for i in range(30):
    tr.circle(r*i)
    tr.up()
    tr.sety((r*i)*(-1))
    tr.down()
turtle.done()

In this output, we can see the concentric circle is drawn on the new drawing board in Python.

Python draw concentric circles using turtle
Python draw concentric circles using turtle

How to draw a spiral square in python turtle

Let us see how we can draw a spiral square in python turtle

  • To draw a spiral square, we have to use the module called import turtle.
  • Here, the length of the side is assigned to variable s. And s = 200 and for loop is used and that loop uses forward() and right() function of turtle module.
  • The spiral is made by reducing the length of the side by a fixed number in each iteration. After that, reduce the length of a side.

Example:

import turtle 
tr = turtle.Turtle()
s = 200
for i in range(100):
    tr.forward(s)
    tr.right(90)
    s = s-2
turtle.done()

In this output, we can see the spiral square is drawn on the new drawing board in Python.

How to draw a spiral square in python turtle
How to draw a spiral square in python turtle

Draw spiral star in python turtle

  • To draw a spiral star, we have to use the module called import turtle.
  • Here, the length of the side is assigned to variable s. And s = 200 and for loop is used and that loop uses forward() and right() function of turtle module.
  • The spiral star is made by reducing the length of the side by a fixed number in each iteration. After that, reduce the length of a side.

Example:

import turtle 
tr = turtle.Turtle()
s = 200
for i in range(100):
    tr.forward(s)
    tr.right(144)
    s = s-2
turtle.done()

In this output, we can see the spiral star is drawn on the new drawing board in Python.

Draw spiral star in python turtle
Draw spiral star in python turtle

Draw a spiral triangle in python turtle

  • To draw a spiral triangle, we have to use the module called import turtle.
  • Here, the length of the side is assigned to variable s. And s = 200 and for loop is used and that loop uses forward() and right() function of turtle module.
  • The spiral triangle is made by reducing the length of the side by a fixed number in each iteration. After that, reduce the length of the side using “s=s-3”.

Example:

import turtle 
tr = turtle.Turtle()
s = 200
for i in range(70):
    tr.forward(s)
    tr.right(120)
    s = s-3
turtle.done()

In this output, we can see the spiral triangle is drawn on the new drawing board in Python.

Draw a spiral triangle in python turtle
Draw a spiral triangle in python turtle

Draw cube in python using turtle

  • To draw a cube in python, we have to use the module called import turtle.
  • The for loop is used to iterate for forming the front square face.
  • To move the turtle, we have used the function forward() and backward().
  • To move the turtle bottom left side we have used “pen.goto(50,50)”
  • Again for loop is used for forming the back square face. Also, we have used “pen.goto(150,50)” and “pen.goto(100,0)” for bottom right side.
  • And for top right side we have used “pen.goto(100,100)” and “pen.goto(150,150)”. For top left side we have used “pen.goto(50,150)” and “pen.goto(0,100)”.

Example:

import turtle
tr = turtle.Screen()
pen = turtle.Turtle()
pen.color("purple")
tr = turtle.Screen()
for i in range(4):
    pen.forward(100)
    pen.left(90)
pen.goto(50,50)
for i in range(4):
    pen.forward(100)
    pen.left(90)
pen.goto(150,50)
pen.goto(100,0)
pen.goto(100,100)
pen.goto(150,150)
pen.goto(50,150)
pen.goto(0,100)
turtle.done()

In this output, we can see that the cube is drawn on the new drawing board in Python.

Draw cube in python using turtle
Draw cube in python using turtle

How to draw a grid in turtle python

  • To draw a grid in turtle python, we have to use the module called import turtle.
  • Set the screen by using “scr=turtle.Screen()” and then make the objects.
  • For drawing the y-axis line we will define a function and then draw a line using the forward method.
  • Now, set the position by using “tr.setpos(value,300)” and then backward is used for another line.
  • For drawing the x-axis lines we will again define a function.
  • Now, we will label the graph grid by defining a function and setting the position.

Example:

import turtle 
scr=turtle.Screen() 
tr=turtle.Turtle()
def draw_y(value):
    tr.forward(300) 
    tr.up() 
    tr.setpos(value,300) 
    tr.down() 
    tr.backward(300) 
    tr.up() 
    tr.setpos(value+10,0) 
    tr.down() 
def draw_x(value): 
    tr.forward(300)
    tr.up() 
    tr.setpos(300,value) 
    tr.down() 
    tr.backward(300) 
    tr.up() 
    tr.setpos(0,value+10) 
    tr.down()
def label(): 
    tr.penup() 
    tr.setpos(155,155) 
    tr.pendown() 
    tr.write(0,font=("Verdana", 12, "bold")) 
    tr.penup() 
    tr.setpos(290,155) 
    tr.pendown() 
    tr.write("x",font=("Verdana", 12, "bold")) 
    tr.penup() 
    tr.setpos(155,290) 
    tr.pendown() 
    tr.write("y",font=("Verdana", 12, "bold")) 
scr.setup(800,800)     
tr.speed(10) 
tr.left(90)   
tr.color('green') 
for i in range(30): 
    draw_y(10*(i+1))
tr.right(90) 
tr.up() 
tr.setpos(0,0) 
tr.down()  
for i in range(30): 
    draw_x(10*(i+1)) 
tr.color('green') 
tr.up() 
tr.setpos(0,150) 
tr.down() 
tr.forward(300) 
tr.left(90) 
tr.up() 
tr.setpos(150,0) 
tr.down() 
tr.forward(300)
label() 
tr.hideturtle()
turtle.done()

In this output, we can see a grid is drawn on the new drawing board in Python.

How to draw a grid in turtle python
How to draw a grid in turtle python

Python turtle graphics not responding

In python turtle, we can face the problem called “python turtle graphics not responding” and eventually the program will end with no graphics output.

Example:

import turtle
tr = turtle.Turtle()
tr.forward(150)

By running the above code, we can see that the turtle window will not get opened. To solve this problem refer to below example.

Example:

This problem occurs because at the end of the code we have to say “turtle.done()” when you are done otherwise you will not get the turtle graphic screen.

import turtle
tr = turtle.Turtle()
tr.forward(150)
turtle.done()

You can refer to the below screenshot, and now you will be able to see the turtle graphics screen by using the above code.

Python turtle graphics not responding
Python turtle graphics not responding

Python turtle mainloop

The mainloop in python turtle make sure that the program continues to run. It is the last statement in the turtle graphics program.

Example:

import turtle
tr = turtle.Turtle()
tr.backward(200)
turtle.mainloop()

In this output, we can see that the backward line is drawn for the interactive use of turtle graphics and the program continues to run.

Python turtle mainloop
Python turtle mainloop

How to activate check if button is pressed on python turtle

  • To activate the check in python, we have to use the module called import turtle.
  • Now, we will define a function “def fun(x,y)”.
  • Here, turtle.onclick(fun) is used to bind the function to a mouse click event on the turtle. When the user will click on the turtle then it will perform the action.

Example:

import turtle 
def fun(x,y):
    turtle.right(90) 
    turtle.forward(150) 
turtle.speed(2) 
turtle.forward(150) 
turtle.onclick(fun)
turtle.done()

In this output, we can see that the line is drawn in a forward direction, and when we will click on the turtle then it will move again, and so on.

How to activate check if button is pressed on python turtle
How to activate check if button is pressed on python turtle

You can refer to below output, to activate check if button is pressed on python turtle.

How to activate check if button is pressed on python turtle

You may like the following Python tutorials:

In this tutorial we have learned about how to draw a different shape in python using turtle and also we saw Turtle programming in Python, also we have covered these topics:

  • What is Turtle in python?
  • How to install turtle in python
  • Python turtle methods
  • Python turtle speed
  • Python turtle speed fastest
  • Change turtle size python
  • Python turtle change pen size
  • Change turtle shape python
  • Python turtle screen size
  • Python turtle how to set position
  • Turtle onscreen click example python
  • How to get coordinate of the screen in python turtle
  • Change the screen title in python turtle
  • Python turtle clear screen
  • How to draw a square in python using turtle
  • How to draw a rectangle in python using turtle
  • How to draw a circle in python using turtle
  • How to draw ellipse in python using turtle
  • Code to draw a star in python turtle
  • Draw a pentagon in python using turtle
  • Draw a hexagon in python turtle
  • Python program to draw Heptagon using turtle
  • Draw octagon in python using turtle
  • Draw a polygon in python using turtle
  • Draw a dot in python using turtle
  • Python draw tangent circles using turtle
  • Python draw spiral circles using turtle
  • Python draw concentric circles using turtle
  • How to draw a spiral square in python turtle
  • Draw spiral star in python turtle
  • Draw a spiral triangle in python turtle
  • Draw cube in python using turtle
  • How to draw a grid in turtle python
  • Python turtle graphics not responding
  • Python turtle mainloop
  • How to activate check if button is pressed on python turtle