Turtle Module in Python
The Turtle module in Python is a built-in module used to draw shapes, patterns, and pictures on the screen.
It works like a pen. We give commands to move the turtle forward, backward, left, or right, and it draws lines on the screen.
Example: We can use the Turtle module to draw a square, triangle, circle, star, or other designs.
How to Import the Turtle Module
First, import the turtle module using the import statement.
import turtle
Code for using turtle module:
GreetingCards.py
import turtle
import random
# Take the candidate's name as input
name = input("Enter your name: ")
# Create the graphics window
screen = turtle.Screen()
screen.setup(900, 650)
screen.title("My Creative Greeting Card")
screen.bgcolor("#101735")
screen.tracer(0)
# Create a turtle for drawing
pen = turtle.Turtle()
pen.hideturtle()
pen.speed(0)
# Function to draw a filled rectangle
def rectangle(x, y, width, height, color):
pen.penup()
pen.goto(x, y)
pen.pendown()
pen.color(color)
pen.begin_fill()
for side in range(2):
pen.forward(width)
pen.left(90)
pen.forward(height)
pen.left(90)
pen.end_fill()
pen.penup()
# Function to draw a star
def star(x, y, size, color):
pen.penup()
pen.goto(x, y)
pen.setheading(0)
pen.pendown()
pen.color(color)
pen.begin_fill()
for i in range(5):
pen.forward(size)
pen.right(144)
pen.end_fill()
pen.penup()
# Function to draw a balloon
def balloon(x, y, color):
pen.penup()
pen.goto(x, y)
pen.setheading(0)
pen.pendown()
pen.color(color, color)
pen.begin_fill()
pen.circle(24)
pen.end_fill()
# Balloon string
pen.penup()
pen.goto(x + 24, y)
pen.setheading(-90)
pen.pendown()
pen.color("white")
pen.forward(65)
pen.penup()
# Draw the outer card
rectangle(-340, -240, 680, 480, "#FF4D8D")
# Draw the inner card
rectangle(-325, -225, 650, 450, "#FFF5D7")
# Draw a colourful decorative banner
rectangle(-325, 145, 650, 80, "#6C42C7")
# Greeting heading
pen.goto(0, 175)
pen.color("white")
pen.write("A SPECIAL GREETING FOR YOU!",
align="center",
font=("Arial", 21, "bold"))
# Draw balloons
balloon(-250, 40, "#FF5252")
balloon(205, 35, "#00BFA6")
balloon(-205, -45, "#448AFF")
balloon(250, -50, "#FFB300")
# Draw colourful stars
star(-270, 150, 35, "#FFD700")
star(235, 150, 35, "#FF5252")
star(-280, -150, 30, "#00BFA6")
star(250, -160, 30, "#448AFF")
# Display the candidate's name
pen.goto(0, 65)
pen.color("#6C42C7")
pen.write("Dear " + name + "!",
align="center",
font=("Arial", 32, "bold"))
# Personal greeting message
pen.goto(0, 5)
pen.color("#E84393")
pen.write("You are AMAZING!",
align="center",
font=("Arial", 25, "bold"))
pen.goto(0, -45)
pen.color("#253858")
pen.write("Keep smiling, keep learning,",
align="center",
font=("Arial", 18, "normal"))
pen.goto(0, -75)
pen.write("and keep shining like a star!",
align="center",
font=("Arial", 18, "normal"))
# Draw colourful confetti
colors = ["red", "blue", "green", "orange",
"purple", "deeppink", "gold"]
for i in range(70):
x = random.randint(-310, 310)
y = random.randint(-210, 130)
# Keep the central text area clear
if -165 < x < 165 and -100 < y < 115:
continue
pen.penup()
pen.goto(x, y)
pen.dot(random.randint(4, 9),
random.choice(colors))
# Signature at the bottom
pen.goto(0, -190)
pen.color("#6C42C7")
pen.write("Made with Python Turtle Graphics",
align="center",
font=("Arial", 13, "italic"))
screen.update()
turtle.done()

