23 Sep turtle.setundobuffer() function in Python
The turtle.setundobuffer() function sets the size of the undo buffer. A larger size allows more actions to be undone. Set to None to disable the undo buffer.
Syntax: turtle.setundobuffer(size)
Parameters: size – an integer (the maximum number of undo entries) or None.
Let us see an example to implement the turtle.setundobuffer() function in Python:
Demo63.py
# turtle.setundobuffer() function in Python
# Code by Studyopedia
import turtle
window = turtle.Screen()
t = turtle.Turtle()
t.setundobuffer(10) # Set undo buffer size to 10 entries
for i in range(15):
t.forward(10)
t.left(10)
entries = t.undobufferentries()
print("Undo buffer entries:", entries)
window.exitonclick()
The following is the output:

In the above code, we followed the below steps:
- Import module: import turtle loads the turtle graphics library.
- Create screen: window = turtle.Screen() opens the drawing window.
- Create turtle: t = turtle.Turtle() creates the turtle you’ll control.
- Set undo buffer size:setundobuffer(10) limits the turtle’s undo history to the 10 most recent undoable actions. Older actions are discarded once the buffer is full.
- Draw with small steps:
for i in range(15): repeats the block 15 times.
forward(10) moves forward 10 units each iteration.
t.left(10) turns left by 10° each iteration.
This produces many undoable actions, exceeding the buffer size, so only the most recent 10 actions are retained. - Get undo buffer entries: entries = t.undobufferentries() returns how many actions are currently stored in the undo buffer (it will cap at 10 given the buffer size).
- Print entries: print(“Undo buffer entries:”, entries) outputs the count to the console.
- Close on click:exitonclick() waits for a mouse click, then closes the window.
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:
- OpenCV Tutorial
- Python Tutorial
- NumPy Tutorial
- Pandas Tutorial
- Matplotlib Tutorial
- Generative AI Tutorial
- LangChain Tutorial
- RAG Tutorial
- Machine Learning Tutorial
- Deep Learning Tutorial
- Ollama Tutorial
- Retrieval Augmented Generation (RAG) Tutorial
- Copilot Tutorial
- Gemini Tutorial
- ChatGPT Tutorial
No Comments