turtle.onscreenclick() 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.onscreenclick()
This function is used to bind fun to a mouse-click event on canvas.
Syntax :
turtle.onscreenclick(fun, btn=1, add=None)
Parameters:
Arguments |
Description |
fun |
a function with two arguments, the coordinates of the clicked point on the canvas. |
btn |
number of the mouse-button defaults to 1 (left mouse button) |
add |
True or False. If True, new binding will be added, otherwise it will replace a former binding |
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(x, y):
global col
ind = random.randint( 0 , 7 )
sc.bgcolor(col[ind])
sc = turtle.Screen()
sc.setup( 400 , 300 )
turtle.onscreenclick(fxn)
|
Output :
Here we can find that whenever the user clicks (yellow-colored dot on arrow) on screen it 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...