20 Sep turtle.ontimer() function in Python
The turtle.ontimer() function arranges for a function to be called after a specified delay (in milliseconds). Useful for creating animations or timed events.
Syntax: turtle.ontimer(fun, t=0)
Parameters: fun – a function with no arguments; t – a number >= 0 (the delay in milliseconds).
Let us see an example to implement the turtle.ontimer() function in Python:
Demo35.py
# turtle.ontimer() function in Python
# Code by Studyopedia
import turtle
window = turtle.Screen()
t = turtle.Turtle()
def blink():
if t.isvisible():
t.hideturtle()
else:
t.showturtle()
window.ontimer(blink, 500) # Schedule next blink in 500ms
blink() # Start blinking
window.mainloop()
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.
- Define blink(): def blink(): toggles the turtle’s visibility and reschedules itself.
- Check visibility: t.isvisible() returns True if the turtle is currently shown.
- Toggle: t.hideturtle() hides it if visible; otherwise t.showturtle() shows it.
- Reschedule: window.ontimer(blink, 500) calls blink again after 500 milliseconds, creating a repeating blink.
- Start blinking: blink() runs the function once to kick off the timer-based loop.
- Run event loop: window.mainloop() keeps the window open and processes the timer callbacks.
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