turtle.clear() function in Python

The turtle.clear() function clears the drawing from the screen. This does not move the turtle or change its state (position, heading, etc.).

Syntax: turtle.clear()
Parameters: None.

Let us see an example to implement the turtle.clear() function in Python:

Demo30.py

# turtle.clear() function in Python
# Code by Studyopedia

import turtle

window = turtle.Screen()
t = turtle.Turtle()

# Draw some shapes
for i in range(5):
    t.forward(50)
    t.left(90)
    
t.clear()               # Clear the turtle's drawings

window.exitonclick()

The following is the output:

turtle.clear() function in Python

In the above code, we followed the below steps:

  1. Import the module: import turtle loads Python’s turtle graphics library.
  2. Create the screen: window = turtle.Screen() opens the drawing window.
  3. Create the turtle: t = turtle.Turtle() creates the turtle (your pen).
  4. Draw with a loop:
    • Repeat 5 times: for i in range(5):
    • Move: t.forward(50) draws a 50-unit line.
    • Turn: t.left(90) rotates 90° counterclockwise. This creates a sequence of right-angled segments.
  5. Clear drawings: t.clear() erases everything this turtle has drawn (including its stamps) from the screen but does not move the turtle or change its heading or pen settings.
  6. Close on click: window.exitonclick() keeps the window open until you click, then closes.

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

turtle.end_fill() function in Python
turtle.onclick() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment