Python has a powerful graphics library called Turtle which can be used to draw geometric shapes, create turtle graphics, and more. In this tutorial, we will learn how to draw objects such as points, circles, ellipses, and rectangles with Python using the Turtle library. The basics of this library are relatively simple to learn, and you can unleash your creativity to create advanced drawings or even simple animations.
Before we can start drawing objects in Python, we need to install the Turtle library. Turtle is a built-in library, which means that it comes with Python, but you may need to install it if you are using an external IDE such as PyCharm. Run the following command on a terminal or command prompt:
1 |
pip install PythonTurtle |
With the Turtle library installed, we can now proceed to create a basic setup where we can draw our objects.
Step 1: Import Turtle Library and Set Up Turtle Screen
Start by importing the turtle library and setting up the turtle screen to draw on.
1 |
import turtle |
Create a turtle screen, where you can draw using the following command:
1 |
screen = turtle.Screen() |
Set the background color of the turtle screen:
1 |
screen.bgcolor("white") |
To set the screen’s title and dimensions:
1 2 |
screen.title("Drawing Objects Example") screen.setup(width=800, height=600) |
Step 2: Create and Configure Turtle Object
Now, create a turtle object that we can use to draw:
1 |
my_turtle = turtle.Turtle() |
Give your turtle object a starting position (x and y coordinates) and orientation (angle). For example:
1 2 |
my_turtle.setpos(0, 0) my_turtle.setheading(0) |
You can also set the color, shape, and other attributes of your turtle object:
1 2 |
my_turtle.pencolor("blue") my_turtle.shape("turtle") |
Step 3: Draw Objects
With our turtle object created and configured, we can proceed to draw our desired objects. In this tutorial, we will learn how to draw a circle, ellipse, and rectangle.
Draw a Circle
To draw a circle, use the following command:
1 |
my_turtle.circle(radius) |
Here, replace radius
with the desired radius of your circle. For example:
1 |
my_turtle.circle(50) |
Draw an Ellipse
To draw an ellipse, use the following loop with specific parameters:
1 2 3 |
for _ in range(2): my_turtle.circle(100, 90) my_turtle.circle(30, 90) |
The ellipse is drawn with two radii, one larger and one smaller.
Draw a Rectangle
To draw a rectangle, use the following loop:
1 2 3 |
for _ in range(4): my_turtle.forward(length) my_turtle.right(90) |
Replace length
with the desired length of your rectangle’s side. For example:
1 2 3 |
for _ in range(4): my_turtle.forward(100) my_turtle.right(90) |
Step 4: Close the Window on Click
Add the following command to close the turtle graphics window when clicked:
1 |
screen.exitonclick() |
Now, run the complete program, and you should see graphical objects on the screen.
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 |
import turtle screen = turtle.Screen() screen.bgcolor("white") screen.title("Drawing Objects Example") screen.setup(width=800, height=600) my_turtle = turtle.Turtle() my_turtle.setpos(0, 0) my_turtle.setheading(0) my_turtle.pencolor("blue") my_turtle.shape("turtle") my_turtle.circle(50) for _ in range(2): my_turtle.circle(100, 90) my_turtle.circle(30, 90) for _ in range(4): my_turtle.forward(100) my_turtle.right(90) screen.exitonclick() |
Output
Conclusion
In this tutorial, we learned how to draw objects such as circles, ellipses, and rectangles using Python’s Turtle library. It is a fantastic library for beginners to learn basic Python syntax and concepts while also providing enough functionality for more advanced users to create elaborate drawings and animations.