Python Turtle Graphics
What is the turtle library in Python?
turtle is a pre-installed Python library that enables users to create pictures and shapes by providing them with a virtual canvas.
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics
How to Use turtle library to make graphics?
- Install and import the library:
pip install PythonTurtle (command for installation)
import turtle (import at the top of project.py file) - Initialize the variable, which you’ll then use throughout the program to refer to the turtle:
c=turtle.Turtle()
you can use any variable instead of “c”. - Now, to open the turtle screen, you initialize a variable for it in the following way:
c.getscreen()
You should see a separate window open up
4. You can change the color of the window using the following command:
c.getscreen().bgcolor(“black”)
5. Changing the Screen Title:
c.title(“My Turtle Program”)
6. Changing the Turtle Shape:
c.shape(“turtle”)
c.shape(“arrow”)
c.shape(“circle”)
7. Change the color of the arrow referring turtle :
c.color(“color”)
e.g.
c.color(“red”,”yellow”)
Change speed:
c.speed(value)
e.g.
c.speed(15)
8 . Drawing Preset Figures:
c.circle(radius)
e.g.
c.circle(60)
Tou will get circle
c.dot(20)
you will get a filled dot
9. Moving the Turtle:
There are four directions that a turtle can move in:
- Forward
- Backward
- Left
- Right
Example :
c.right(90)
c.forward(100)
c.left(90)
c.backward(100)
You can use the shortened versions of these commands as well:
- c.rt() instead of c.right()
- c.fd() instead of c.forward()
- c.lt() instead of c.left()
- c.bk() instead of c.backward()
10. Filling in an Image:
c.begin_fill()
c.fd(100)
c.lt(120)
c.fd(100)
c.lt(120)
c.fd(100)
c.end_fill()
Python Turtle Projects: