Thursday, January 22, 2026
HomeLanguagesturtle.onclick() function in Python

turtle.onclick() 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.onclick()

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)

Parameters:

Arguments Description
fun a function with two arguments, to which will be assigned 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, 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 :

Python3




# import package
import turtle
  
  
# method to action
def fxn(x,y):
      
    # some motion
    turtle.right(90)
    turtle.forward(100)
  
# turtle speed to slowest
turtle.speed(1)
  
# motion
turtle.fd(100)
  
# allow user to click 
# for some action
turtle.onclick(fxn)


Output :

Example 2 :

Python3




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


Output :

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12064 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7220 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS