Draw Indian Flag using Turtle library in Python

Turtle is a pre-installed Python library that enables users to create pictures and shapes by providing them with a virtual canvas. Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon in 1967.
The onscreen pen that you use for drawing is called as turtle. The turtle has three attributes: a location, an orientation (or direction), and a pen.

How to Use turtle library to make graphics?

1. Install and import the library:

  • pip install PythonTurtle (command for installation)
  • import turtle (import at the top of project.py file)

2. Initialize the variable, which you’ll then use throughout the program to refer to the turtle:

  • a=turtle.Turtle()
  • You can use any variable instead of “a”.

3. Now, to open the turtle screen, you initialize a variable for it in the following way:

  • a.getscreen()
  • You should see a separate window open up.

4. You can change the color of the window using the following command:

  • a.getscreen().bgcolor(“black”)
  • It will change the background color to black.

Moving the Turtle Head

The turtle can move in four directions:
  • Forward
  • Backward
  • Left
  • Right

Draw National Flag Of India Using Turtle:

import turtle

flag = turtle.Turtle()

flag.speed(3)
flag.pensize(5)
flag.color(‘#000080’)

def draw(xy):
    flag.penup()
    flag.goto(x,y)
    flag.pendown()


#Ashoka Chakra in middle
for i in range(24):
    flag.forward(80)
    flag.backward(80)
    flag.left(15)
draw(0-80)
flag.circle(80360)

draw(0,-90)

#Green Rectangle in bottom of flag
flag.color(‘#138808’)
flag.begin_fill()

flag.forward(350)
flag.backward(700)
flag.right(90)
flag.forward(200)
flag.left(90)
flag.forward(700)
flag.left(90)
flag.forward(200)
flag.left(90)

flag.end_fill()


#Orange Rectangle in upper part of flag
flag.color(‘#FF8F1C’)
draw(-350,90)

flag.begin_fill()

flag.right(180)
flag.forward(700)
flag.left(90)
flag.forward(200)
flag.left(90)
flag.forward(700)
flag.left(90)
flag.forward(200)

flag.end_fill()

flag.hideturtle()

turtle.done()

Output:

Refrence:

Leave a Reply

Your email address will not be published. Required fields are marked *