Celebrating India's Independence Day with Python and Tkinter
Independence Day is a time for reflection, celebration, and pride for Indians all over the world. On this auspicious day, we honour the valiant sacrifices of our freedom fighters and look forward to a future filled with hope and possibilities.
In this blog, we will combine the spirit of Independence Day with the joy of coding by creating a visual representation of the Indian flag using Python's Tkinter library. Whether you're new to programming or have been coding for years, this is a great way to celebrate the day by learning something new. Let's get started!
Setting the Stage
To begin, we will use the tkinter
library in Python. Tkinter
is a standard GUI (Graphical User Interface) toolkit in Python and provides a powerful object-oriented interface to the Tkinter GUI toolkit.
Let's break down the code step by step.
1. Import Necessary Libraries:
import tkinter as tk
from math import sin, cos, radians
We start by importing the necessary modules:
tkinter
for creating our graphical window.Essential math functions (
sin
,cos
,radians
) to help draw the Ashoka Chakra's spokes.
2. Drawing the Ashoka Chakra:
Let's make a function solely focusing on drawing the Ashoka Chakra.
Function Definition
def draw_chakra(canvas, center_x, center_y, radius):
The function draw_chakra
is designed to draw the Ashoka Chakra. It takes in four parameters:
canvas
: The drawing surface on which the Chakra will be drawn.center_x
andcenter_y
: The x and y coordinates, respectively, of the center of the Chakra.radius
: The radius of the Chakra.
Drawing the Blue Circle
canvas.create_oval(center_x - radius, center_y - radius, center_x + radius, center_y + radius, fill='#ffffff', outline='#000080')
This line draws an oval (when given the coordinates, it will be a circle) with the specified radius on the canvas.
The
create_oval
method requires two pairs of coordinates (the top-left and the bottom-right corners of a rectangle) within which the oval will be inscribed. By subtracting and adding the radius to the center's coordinates, we define this bounding rectangle.fill='#ffffff'
: This sets the inside colour of the oval to white.outline='#000080'
: This sets the outline colour of the oval to blue (Ashoka Chakra's colour).
Drawing the 24 Spokes
for i in range(24):
angle = radians(i * 15)
To draw 24 spokes equally spaced, we loop 24 times.
angle = radians(i * 15)
: This line calculates the angle of each spoke. Since there are 24 spokes in 360ยฐ, each spoke is360/24 = 15
degrees apart. We convert this angle from degrees to radians as thesin
andcos
functions in Python use radians.
start_x = center_x + (10 * cos(angle)) # Minor adjustment to the starting point
start_y = center_y - (10 * sin(angle))
end_x = center_x + (radius * cos(angle))
end_y = center_y - (radius * sin(angle))
Using trigonometry, we calculate each spoke's start and end coordinates.
The starting point of each spoke isn't the exact center of the circle, but a bit outside, which is why we have a minor adjustment of
10 * cos(angle)
and10 * sin(angle)
.The ending points are calculated by extending a line from the center to the circle's circumference.
canvas.create_line(start_x, start_y, end_x, end_y, fill='#000080', width=3)
This line draws the spoke on the canvas using the starting and ending coordinates.
fill='#000080'
: The colour of the spoke is blue.width=3
: The line width of the spoke is set to 3 pixels.
Drawing the Small Circle at the Center
small_circle_radius = radius / 7
canvas.create_oval(center_x - small_circle_radius, center_y - small_circle_radius, center_x + small_circle_radius, center_y + small_circle_radius, fill='#000080')
First, we determine the radius of the small circle. It's set to be one-seventh of the Chakra's main radius.
Then, similar to the large blue circle earlier, we draw an oval. In this case, it's a blue circle inscribed in a bounding rectangle.
3. Drawing the Indian Flag:
In the main
function, we set up the window and the canvas where we will draw the flag.
root = tk.Tk()
root.title("Flag of India")
canvas = tk.Canvas(root, width=600, height=400, bg='white')
canvas.pack()
The flag consists of three horizontal bands: saffron at the top, white in the middle with the Ashoka Chakra, and green at the bottom.
# Saffron colour band
canvas.create_rectangle(0, 0, 600, 133, fill='#FF9933', outline='#FF9933')
# White colour band (with Ashoka Chakra)
canvas.create_rectangle(0, 133, 600, 266, fill='#FFFFFF', outline='#FFFFFF')
draw_chakra(canvas, 300, 200, 65)
# Green colour band
canvas.create_rectangle(0, 266, 600, 400, fill='#138808', outline='#138808')
We use the create_rectangle
function to draw the bands, specifying the top-left and bottom-right corner coordinates and the colour for each band.
4. Wrapping Up:
The line root.mainloop()
starts the Tkinter event loop, making the window persistent until it's closed.
Finally, the condition if __name__ == '__main__':
ensures that the main
function will be called only when this script is run directly (and not imported as a module).
Preview:
Complete code available at:
๐ github.com/saifeemustafaq/tiranga
Some facts about the Indian flag:
The Indian National Flag, often affectionately referred to as the "Tiranga" meaning "Tricolor", is an embodiment of India's sovereignty, pride, and history. Here are some special facts about the Indian Flag:
Tricolor Design: The Indian flag has three horizontal bands of different colours:
Saffron at the top, which symbolizes sacrifice and bravery.
White in the middle, representing truth, peace, and purity.
Green at the bottom, symbolizing prosperity and faith.
Ashoka Chakra: In the center of the white band, there's a navy-blue wheel with 24 spokes, known as the Ashoka Chakra. It represents the eternal wheel of law and was inspired by the Lion Capital of Ashoka โ a sculpture that was erected by Emperor Ashoka in Sarnath, Uttar Pradesh.
Manufacturing Standards: The cloth for the flag was originally sourced from the Khadi Development and Village Industries Commission, a subsidiary of the Indian Government. The idea was to promote the use of Khadi โ a hand-spun cloth, which has historical significance in India's struggle for freedom, led by Mahatma Gandhi.
Evolution: The Constituent Assembly of India adopted the national flag's current design on 22nd July 1947. However, it has its roots in the flag designs used during the Indian Nationalist movement/Indian Independence movement.
Designer: The present-day flag design was created by Pingali Venkayya, an Indian freedom fighter.
The Indian National Flag isn't just a piece of cloth but a symbol of India's rich history, its struggles, its victories, and the hopes and aspirations of its people. When hoisted high, it invokes a deep sense of pride and patriotism among Indians worldwide.
Conclusion:
There you have it! A beautiful representation of the Indian flag using Python's Tkinter. Through this exercise, not only did we celebrate our nation's pride but also got hands-on with Python programming. Whether it's Independence Day or any other day, let's keep the spirit of learning and patriotism alive!
Wishing everyone a Happy Independence Day! ๐ฎ๐ณ๐๐