Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
turtle   0   28561
Python Script 12: Drawing Indian National Flag Tricolor using Python Turtle

This is the 12th script in the series Python Scripts and third script where we are creating something using turtle.

Code is self explanatory as I have added plenty of comments within code. 

Before proceeding further, it is important to understand the coordinates and quadrants. Refer the diagram below.


turtle coordindate python


We have taken the flag dimensions ratio into consideration while writing program. Still if there are any inaccuracies, please consider this tutorial for educational purpose only.


Code:

#
# Python script to create Indian flag using turtle.
# Author - https://www.pythoncircle.com
# 

import turtle
import time

# create a screen
screen = turtle.getscreen()
# set background color of screen
screen.bgcolor("#b3daff")
# set tile of screen
screen.title("Indian Flag - https://www.pythoncircle.com")
# "Yesterday is history, tomorrow is a mystery, 
# but today is a gift. That is why it is called the present.”
# — Oogway to Po, under the peach tree, Kung Fu Panda Movie
oogway = turtle.Turtle()
# set the cursor/turtle speed. Higher value, faster is the turtle
oogway.speed(100)
oogway.penup()
# decide the shape of cursor/turtle
oogway.shape("turtle")

# flag height to width ratio is 2:3
flag_height = 300
flag_width = 450

# starting points
# start from the first quardant, half of flag width and half of flag height
start_x = -225
start_y = 150

# For saffron, white and green stripes. each strip width will be flag_height/3 = 100
stripe_height = flag_height/3
stripe_width = flag_width

# Radius of Ashok Chakra, half of white stripe 
chakra_radius = stripe_height / 2


def draw_fill_rectangle(x, y, height, width, color):
    oogway.goto(x,y)
    oogway.pendown()
    oogway.color(color)
    oogway.begin_fill()
    oogway.forward(width)
    oogway.right(90)
    oogway.forward(height)
    oogway.right(90)
    oogway.forward(width)
    oogway.right(90)
    oogway.forward(height)
    oogway.right(90)
    oogway.end_fill()
    oogway.penup()


# this function is used to create 3 stripes
def draw_stripes():
    x = start_x
    y = start_y
    # we need to draw total 3 stripes, 1 saffron, 1 white and 1 green
    draw_fill_rectangle(x, y, stripe_height, stripe_width, "#FF9933")
    # decrease value of y by stripe_height
    y = y - stripe_height   
    # create middle white stripe
    draw_fill_rectangle(x, y, stripe_height, stripe_width, "white")
    y = y - stripe_height               

    # create last green stripe
    draw_fill_rectangle(x, y, stripe_height, stripe_width, '#138808')
    y = y - stripe_height


def draw_chakra():
    oogway.speed(1)
    oogway.goto(0,0)
    color = "#000080" # navy blue
    oogway.penup()
    oogway.color(color)
    oogway.fillcolor(color)
    oogway.goto(0, 0 - chakra_radius)
    oogway.pendown()
    oogway.circle(chakra_radius)
    # draw 24 spikes in chakra
    for _ in range(24):
        oogway.penup()
        oogway.goto(0,0)
        oogway.left(15)
        oogway.pendown()
        oogway.forward(chakra_radius)
  
    

# start after 5 seconds.
time.sleep(0)
# draw 3 stripes
draw_stripes()
# draw squares to hold stars
draw_chakra()
# hide the cursor/turtle
oogway.hideturtle()
# keep holding the screen until closed manually
screen.mainloop()


Copy the above code in india_flag_turtle.py file and run it using command python3 india_flag_turtle.py. 

If you get below error while running the script, you need to install python-tk.

raise ImportError, str(msg) + ', please install the python-tk package'
ImportError: No module named _tkinter, please install the python-tk package

Command to install python-tk in Linux

sudo apt-get install python-tk


Code is available on github.


Video:




References:

[1] https://www.pythoncircle.com/post/661/python-script-11-drawing-flag-of-united-states-of-america-using-python-turtle/

turtle   0   28561
0 comments on 'Python Script 12: Drawing Indian National Flag Tricolor Using Python Turtle'

Related Articles:
Python Script 11: Drawing Flag of United States of America using Python Turtle
Python turtle code to create United States of America flag. Drawing USA flag using turtle in python....
Python Script 6: Wishing Merry Christmas using Python Turtle
Python script to wish merry Christmas using python turtle. Using Python Turtle module to wish merry Christmas to your friends....
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap