Skip to content
Related Articles

Related Articles

turtle.onclick() function in Python
  • Last Updated : 26 Jul, 2020

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.


This function is used to bind fun to a mouse-click event on this turtle or on canvas. 

Syntax :

turtle.onclick(fun, btn=1, add=None)


funa function with two arguments, to which will be assigned the coordinates of the clicked point on the canvas
btnnumber of the mouse-button defaults to 1 (left mouse button)
addTrue or False. If True, the new binding will be added, otherwise, it will replace a former binding

Below is the implementation of the above method with some examples :

Example 1 :


# import package
import turtle
# method to action
def fxn(x,y):
    # some motion
# turtle speed to slowest
# motion
# allow user to click 
# for some action

Output :

Example 2 :


# import package
import turtle
# screen object
wn = turtle.Screen()
# method to perform action
def fxn(x, y):
  turtle.goto(x, y)
# onclick action 

Output :

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :