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.

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()
# draw 24 spikes in chakra
for _ in range(24):
oogway.penup()
oogway.goto(0,0)
oogway.left(15)
oogway.pendown()

# 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.

References:

Related Articles:
Validating json using python code, format and beautify json file using python, validate json file using python, how to validate, format and beautify json...
Collecting one million website links by scraping using requests and BeautifulSoup in Python. Python script to collect one million website urls. Using beautifulsoup to scrape data...
Generating ascii art from image, converting colored image to ascii code, python script to convert image to ascii code, python code to generate the ascii image from jpg image....
Python script to convert the ebooks from one format to another in bulk. Automated book conversion to kindle format. Free kindle ebook format conversion....

SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.

Categories:
Tags: