Python’s Turtle Graphics module is one of the most fun ways to bring coding to life. With just a few lines of code, you can make your turtle draw shapes, patterns, and even full pictures. In this tutorial, we’ll use Python Turtle inside the Trinket IDE to create a flower with petals, a stem, and leaves, all set against a sunflower background.
✨ What You’ll Need
- A free Trinket.io account.
- Basic knowledge of Python functions and loops.
- The Turtle module (already built into Python).
🌼 Step 1: Setting the Scene
We’ll start by creating a new trinket and setting up our canvas, giving it a background image to make the drawing pop. In this case, we’ll use a sunflower background (you’ll need to download an image of your own – try searching on Pixabay.com):
import turtle
# Set up the screen
screen = turtle.Screen()
screen.bgpic("sunflower.jpg")
🌼 Step 2: Meet Our Artist
Next, we create the turtle pen that will do all the drawing:
# Create the turtle
pen = turtle.Turtle()
pen.shape("turtle")
pen.speed(20)
We set the turtle shape to "turtle" (just for fun!) and increase the speed for smoother drawing (you can decrease the speed by changing this value to a smaller number).
🌼 Step 3: Drawing a Petal
Flowers are made of petals, so we’ll write a function to draw a single petal. This function uses circles and angles to create a petal shape:
def draw_petal():
pen.begin_fill()
pen.circle(100, 60) # Arc
pen.left(120)
pen.end_fill()
pen.circle(100, 60)
pen.left(120)
🌼 Step 4: Building the Flower
To form the flower, we draw 36 petals, turning slightly after each one:
pen.color("darkorange")
pen.penup()
pen.goto(0, 60)
pen.pendown()
for _ in range(36):
draw_petal()
pen.left(10)
This loop creates a full circular arrangement of petals—like a sunflower or daisy!
🌼 Step 5: Adding the Flower Center
Every flower needs a center. We’ll draw a yellow circle in the middle:
pen.goto(0, 20)
pen.color("yellow")
pen.begin_fill()
pen.circle(40)
pen.end_fill()
🌿 Step 6: Drawing the Stem and Leaves
Finally, we add a green stem and two leaves using larger circles and arcs:
# Stem
pen.goto(0, 15)
pen.right(90)
pen.color("green")
pen.pensize(10)
pen.forward(200)
# Left leaf
pen.right(150)
pen.forward(80)
pen.right(45)
pen.begin_fill()
pen.circle(80, 90)
pen.left(90)
pen.circle(80, 90)
pen.end_fill()
pen.right(45)
pen.forward(80)
# Right leaf
pen.penup()
pen.left(120)
pen.pendown()
pen.forward(80)
pen.right(45)
pen.begin_fill()
pen.circle(80, 90)
pen.right(-90)
pen.circle(80, 90)
pen.end_fill()
🌻 The Final Touch
At the end, we hide the turtle so that only our artwork remains:
pen.hideturtle()
🎨 The Result
When you run the program in Trinket, you’ll see a flower with orange petals, a yellow center, a green stem, and two leaves, on top of your sunflower background.
This is a great project to learn about:
- Loops (drawing multiple petals).
- Functions (reusable code for each petal).
- Positioning with
penup() and pendown().
- Colors and fills to make drawings vibrant.
🚀 Next Challenge
Try experimenting with:
- Changing the number of petals.
- Using different colors.
- Adding more leaves or flowers.
- Refactor or improve the code!
👉 With just Python and Turtle, you can create stunning visual art while learning the fundamentals of programming. Keep experimenting and watch your garden grow! 🌸
Latest Posts