Group1 Turtle Graphics
Group1 Turtle Graphics
2
4 6
3 5 7
By combining together these and similar commands, intricate shapes and pictures.
The turtle has three attributes: a location, an orientation (or direction), and a pen. The pen, too, has attributes: color, width, and
on/off state (also called down and up).
turtle starting at (0, 0) in the x-y plane. After an import turtle, give it the command turtle.forward(15), and it moves (on-screen!) 15
pixels in the direction it is facing, drawing a line as it moves. Give it the command turtle.right(25), and it rotates in-place 25
degrees clockwise.
Example
import turtle
my_turtle = turtle.Turtle()
my_turtle.forward(105)
my_turtle.right(25)
turtle.mainloop()
24.1.Introduction
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it
uses tkinter for support the Turtle
Turtle method:
To use multiple turtles on a screen one has to use the object-oriented interface.
Example
import turtle
screen = tur tle. Screen()
raw_tur tle = turtle. RawTurtle(screen)
for _ in r ange( 4):
raw_tur tle. forward(100)
raw_tur tle. right(90)
screen. mainloop()
Final Example
fr om turtle import *
color ('red', 'yellow')
begin_f ill()
while True:
forward(200)
left( 170)
if abs(pos( )) < 1:
br eak
end_fill()
done()
In the Python turtle module, the RawTurtle and Turtle classes are used for creating and manipulating turtle graphics. These classes provide various methods and
corresponding functions for drawing shapes, moving the turtle cursor, changing attributes like color and pen size, and more. Here's an overview of some common methods
24.1.3. Methods of RawTurtle/Turtle and corresponding
and their corresponding functions:
• Movement Commands:
⚬ forward(distance): Move the turtle forward by the specified distance.
⚬ backward(distance): Move the turtle backward by the specified distance.
• Turning Commands:
⚬ right(angle): Turn the turtle right by the specified angle.
⚬ left(angle): Turn the turtle left by the specified angle.
3.Setting Speed:
⚬ speed(speed=None): Set the turtle’s speed to an integer value in the range 0..10 or a
speed string.
4.Turtle.dot
is a function that draws a dot at the turtle's current position.
5.Turtle.undo
function in Python's Turtle module is used to undo the last action performed by the turtle.
24.1.3.2. Settings for measurement
In the Python turtle module, you can adjust settings related to measurement units and angles
Degree
The default unit for measuring angles in the Turtle module is degrees. This means that angles specified in
rotation functions like turtle.left() and turtle.right() are interpreted as degrees.
24.1.3.3. Pen control
Pen control in the Python turtle module refers to commands that control the drawing behavior of the turtle's
pen.
Drawing state
In the Turtle module of Python, the drawing state refers to whether the turtle's pen is currently down (drawing) or up
(not drawing).
turtle.penup(): This is useful for repositioning the turtle without drawing lines..
24.1.3.3. Pen control
turtle.pendown(): When the pen is down, the turtle draws lines as it moves.
Color control
In Python's Turtle module, color control refers to the ability to set and manipulate the color used
by the turtle for drawing on the screen.
Turtle pencolor
turtle.pencolor() function is used to set the color of the pen that the turtle
Color control
Turtle fillcolor
turtle.fillcolor(color) function is used to set the fill color that the turtle uses when filling shapes.
24.1.3.4.4. More drawing control
Turtle. reset The turtle.reset() function is a command used in the Python Turtle graphics library. When called, it
resets the turtle's position and other attributes to their default values.
24.1.3.5. Turtle state
24.1.3.5.1. Visibility
Make the turtle invisible. It’s a good idea to do this while you’re in the middle of doing some complex
drawing, because hiding the turtle speeds up the drawing observably.
Panha
24.1.3.5.2. Appearance
• The turtle.shape()
function is used to change the shape of this cursor. The shapes can be “arrow”, “turtle”, “circle”, “square
”, “triangle”, or "classic".
OUTPUT
24.1.3.5.2. Appearance
The turtle.resizemode() function in Python’s turtle module is used to change the size of the
turtle.
OUTPUT
24.1.3.5.2. Appearance
The turtle.turtlesize() this function is used to return or set the pen’s attributes x or y-
stretchfactors.
OUTPUT
24.1.3.6. Using event
Events in Python’s turtle module are used to make the program interactive and responsive t
o user actions such as mouse clicks or keyboard inputs
.
• turtle.onclick() :This function is used to bind fun to a mouse-click event on this turtle.
OUTPUT
24.1.3.6. Using event
• turtle.onrelease() :This function is used to bind fun to the mouse-button-release event
on this turtle.
OUTPUT
24.1.3.6. Using event
• turtle.ondrag() :This function is used to bind fun to mouse-move event on this turtle on
canvas.
OUTPUT
24.1.4. Methods of TurtleScreen/Screen and corresponding
functions
import turtle
This function is used to return the list of
screen = turtle.Screen()
turtles on the screen. This doesn’t require any screen.title("My Turtle Window")
argument. screen.bgcolor("lightblue")
screen.setup(width=800, height=600)
def on_click(x, y):
print(f"Clicked at ({x}, {y})")
screen.onclick(on_click)
turtle.done()
sambo
24.1.4.2. Animation control
• Animations are a great way to make Visualizations more attractive and user appealing. It helps us to demonstrate
Data Visualization in a Meaningful Way, you can use functions like forward(), backward(), left(), right(), etc.,
along with loops to create animated sequences.
sambo
24.1.4.3. Using screen events
import turtle
You can use functions like onscreenclick() or onkey() to bind event handlers to # Function to be called when the screen is clicked
specific actions. def on_click(x, y):
print(f"Mouse clicked at ({x}, {y})")
• onscreenclick() is used to bind the on_click function to the left mouse # Create turtle screen
screen = turtle.Screen()
button click event. The on_click function moves the turtle to the clicked
# Set the screen title
position and draws a blue dot. screen.title("Turtle Screen Events")
• onkey() This function is used to bind fun to the key-release event of the # Set the screen click handler
• listen() is used to make the screen listen for events. # Listen to screen events
screen.listen()
sambo
24.1.4.4. Input methods
• The two primary input methods in Turtle are textinput() and numinput()
• textinput(title, prompt): This method displays a prompt to the user and waits for the user to enter text. It returns the
entered text as a string.
import turtle
# Get user input as text
user_input = turtle.textinput("Text Input", "Enter your name:")
print("User entered:", user_input)
turtle.done()
• numinput(title, prompt, default=None, minval=None, maxval=None): This method is used to get numeric
input from the user.
import turtle
# Get user input as a number
user_input = turtle.numinput("Number Input", "Enter your age:", default=25, minval=1, maxval=100)
print("User entered:", user_input)
turtle.done()
sambo
24.1.4.5. Settings and special methods
• settings and special methods to control the turtle's behavior and appearance. Some key settings include speed(),
bgcolor(), pencolor(), pensize(), and shape(), allowing you to customize the drawing environment. Special
methods like forward(), backward(), left(), right(), penup(), pendown(), and circle() enable you to control the
turtle's movements and drawing actions.
• Example
sambo
24.1.4.6. Methods specific to Screen, not inherited from TurtleScreen
• Key methods include bgcolor() for setting the background color, title() for setting the window
title, screensize() for adjusting the window dimensions, setup() for configuring the window's
width, height, start position, and coordinates, tracer() for managing animation, and update() for
forcing a screen update.
• Example
sambo
24.1.5. Public classes
24.1.6. Help and configuration
24.1.6.1. How to use help
• help() function provides documentation and explanations about the turtle module’s functions,
methods, and classes. It’s a valuable resource for understanding and learning how to use turtle
graphics effectively.
24.1.6.2. Translation of docstrings into different
languages
Create and write docstring-dictionary to a Python script with the given filename. This function has to be
called explicitly (it is not used by the turtle graphics classes). The docstring dictionary will be written to
the Python script filename.py. It is intended to serve as a template for translation of the docstrings into
different languages.
If you (or your students) want to use turtle with online help in your native language, you have to translate
the docstrings and save the resulting file as e.g. turtle_docstringdict_german.py.
Example:
turtle.write_docstringdict(filename="turtle_docstringdict")
+ Setting up the Screen Size and Position + Creating and Configuring a Turtle
24.1.7
24.1.7. Demo scripts
• A set of 15 demo articles showing various features of the new Turtle theme. The
Turtledemo.two_canvases example shows how to use two canvases at once with the turtle module.
This directory contains the Turtle.cfg file, as an example of how to create and use these files.
Bunham
24.1.8
24.1.8. Changes since Python 2.6
• A process with this name and this role is available as a Screen process. (Yes, in Python 2.6, this method
is just a duplicate of the related TurtleScreen/Screen method.) The Turtle.fill() method has been
deprecated. Added Turtle.filling() method. This behavior is similar to the parameterless fill() call in
Python 2.6.
Bunham
24.1.9
24.1.9. Changes since Python 3.0
Bunham
THANK YOU!