Tt
Click this widget to change the font size.
CC
Click this widget to change contrast.

Home Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Links | Search | Bio |

Guide to Python (University at Buffalo Version)


Chapter 9: Python Turtle Graphics

Using Python Turtle Graphics

Python provides a graphical drawing tool called Turtle graphics. To begin using Turtle, you must first import turtle. Below are a few examples.

# first_turtle.py

import turtle

# Set the window size
turtle.setup(300, 100)
# Set the window title
turtle.title('My First Turtle')
# set the pen color
turtle.pencolor("Dark Orange")
# Move the turtle
turtle.forward(50)
Figure 9-1: Python Using Turtle Code
fig9-2

Figure 9-2: Python Using Turtle IDLE Output
# first_turtle.py

import turtle

def main():
    turtle.setup(200, 300)
    # Set the window title
    turtle.title('My First Turtle')
    # set the pen color
    turtle.pencolor("Dark Orange")
    # Move the turtle
    turtle.forward(50)
    for i in range(100, 110):
        turtle.left(90)
        turtle.forward(i)
        turtle.left(5)
main()
# keep window open
turtle.Screen().exitonclick()
Figure 9-3: Python Using Turtle Code
fig9-4

Figure 9-2: Python Using Turtle IDLE Output

Help contribute to my OER Resources. Donate with PayPal button via my PayPal account.
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2025 Jim Gerland