turtle.ontimer() function in Python
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support.
turtle.ontimer()
This function is used to install a timer, which calls fun after t milliseconds.
Syntax :
turtle.ontimer(fun, t=0)
Parameters:
Arguments |
Description |
fun |
a function with no arguments |
t |
a number >= 0 |
Below is the implementation of the above method with an example :
Python3
import turtle
import random
col = [ 'red' , 'yellow' , 'green' , 'blue' ,
'white' , 'black' , 'orange' , 'pink' ]
def fxn():
global col
ind = random.randint( 0 , 7 )
sc.bgcolor(col[ind])
sc = turtle.Screen()
sc.setup( 400 , 300 )
for i in range ( 10 ):
turtle.ontimer(fxn, t = 400 * (i + 1 ))
|
Output :
Here we can find that after some time (set by timer) it automatically changes the background color of the turtle graphics window randomly.
Last Updated :
26 Jul, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...