In this tutorial, we will learn how to create a simple leaf drawing using Python. We’ll be using the popular graphics library, Turtle. The Turtle graphics library provides a simple and easy-to-use interface for drawing shapes and graphics. By the end of this tutorial, you’ll be able to draw a leaf on your screen using Python code.
Step 1: Install the Turtle library
If you are using Python 3.1 or above, the turtle library comes pre-installed. If not, you can install it using the following command:
1 |
pip install PythonTurtle |
Step 2: Setting up the turtle screen
First, let’s start by importing the turtle library and setting up the turtle screen.
1 2 3 4 5 6 7 |
import turtle # Setting up the turtle screen screen = turtle.Screen() screen.title("Leaf Drawing") screen.setup(800, 600) screen.bgcolor("white") |
Step 3: Initialize the turtle
Now, let’s initialize the turtle object which we’ll be using for drawing the leaf.
1 |
# Initialize the turtle<br>leaf_turtle = turtle.Turtle()<br>leaf_turtle.pensize(3) |
Step 4: Draw the leaf
Now, we’ll create a function called draw_leaf() that will have the commands necessary to draw a leaf on the screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
def draw_leaf(): leaf_turtle.color("forest green") leaf_turtle.begin_fill() leaf_turtle.right(60) leaf_turtle.forward(100) leaf_turtle.left(120) leaf_turtle.forward(100) leaf_turtle.right(120) leaf_turtle.circle(50, 180) leaf_turtle.right(60) leaf_turtle.circle(50, 180) leaf_turtle.right(120) leaf_turtle.forward(100) leaf_turtle.left(120) leaf_turtle.forward(100) leaf_turtle.left(60) leaf_turtle.end_fill() leaf_turtle.hideturtle() |
Step 5: Execute the functions
Finally, let’s execute the functions and display the output on the screen.
1 |
draw_leaf()<br>turtle.done() |
Here is the full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import turtle # Setting up the turtle screen screen = turtle.Screen() screen.title("Leaf Drawing") screen.setup(800, 600) screen.bgcolor("white") # Initialize the turtle leaf_turtle = turtle.Turtle() leaf_turtle.pensize(3) def draw_leaf(): leaf_turtle.color("forest green") leaf_turtle.begin_fill() leaf_turtle.right(60) leaf_turtle.forward(100) leaf_turtle.left(120) leaf_turtle.forward(100) leaf_turtle.right(120) leaf_turtle.circle(50, 180) leaf_turtle.right(60) leaf_turtle.circle(50, 180) leaf_turtle.right(120) leaf_turtle.forward(100) leaf_turtle.left(120) leaf_turtle.forward(100) leaf_turtle.left(60) leaf_turtle.end_fill() leaf_turtle.hideturtle() draw_leaf() turtle.done() |
Output
Conclusion
In this tutorial, we learned how to draw a leaf using Python’s turtle library. You can now play around with the code to create different shapes or modify the leaf drawing as per your requirements. The turtle library provides endless possibilities to create graphical designs using simple Python commands.