Python’s Turtle graphics is a great way to learn programming while creating visual projects. In this tutorial, weโll use Turtle to draw the Indian flag with its saffron, white, and green stripes, along with the Ashoka Chakra at the center. ๐ฎ๐ณโจ
Prerequisites
Before getting started, ensure you have Python installed on your system. Turtle is a built-in module, so no additional installations are required.
Steps to Draw the Indian Flag
- Set up the Turtle environment โ Create a drawing screen with a white background.
- Draw the three horizontal stripes โ Saffron at the top, white in the middle, and green at the bottom.
- Draw the Ashoka Chakra โ A navy blue wheel with 24 equally spaced spokes.
- Position the elements correctly โ Use precise coordinates for accurate representation.
Python Code to Draw the Indian Flag
import turtle
def draw_rectangle(color, x, y, width, height):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.color(color)
turtle.begin_fill()
for _ in range(2):
turtle.forward(width)
turtle.right(90)
turtle.forward(height)
turtle.right(90)
turtle.end_fill()
def draw_chakra():
turtle.penup()
turtle.goto(0, -20)
turtle.pendown()
turtle.color("navy")
turtle.width(2)
turtle.circle(30)
# Draw 24 spokes
for _ in range(24):
turtle.penup()
turtle.goto(0, 13)
turtle.pendown()
turtle.forward(30)
turtle.backward(30)
turtle.right(15)
def draw_indian_flag():
screen = turtle.Screen()
screen.bgcolor("white")
screen.title("Indian Flag")
turtle.speed(3)
# Draw Saffron Rectangle
draw_rectangle("orange", -180, 100, 360, 60)
# Draw White Rectangle
draw_rectangle("white", -180, 40, 360, 60)
# Draw Green Rectangle
draw_rectangle("green", -180, -20, 360, 60)
# Draw Ashoka Chakra
draw_chakra()
# Hide Turtle
turtle.hideturtle()
turtle.done()
# Run the function
draw_indian_flag()
Understanding the Code
- Drawing the Flag Stripes: We use a function to draw three colored rectangles representing saffron, white, and green.
- Drawing the Ashoka Chakra: The
draw_chakra()
function uses a circle and loops to create 24 spokes. - Using Turtle Functions:
penup()
,pendown()
,goto()
, andcircle()
are used to position and draw elements.
Output
Running this script will generate an Indian flag with the correct proportions and the Ashoka Chakra at its center. ๐จ
Conclusion
This Python Turtle project is an excellent way to practice graphics programming while celebrating Indiaโs national pride. Modify and experiment with the colors, size, or add animations to make it even more engaging!
๐ Enjoyed this tutorial? Like, share, and subscribe for more exciting coding projects!
๐ Additional Resources
๐ Video Tutorial:
๐ Code Repository: GitHub Source Code
#Python #IndianFlag #TurtleGraphics #LearnPython #Programming #TechTutorial ๐๐ฅ