Open In App

turtle.onclick() function in Python

Last Updated : 26 Jul, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 :



Next Article

Similar Reads

turtle.forward() method in Python-Turtle
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.forward() The turtle.forward() method is used to move the turtle forward by the value of the argument that it takes. It gives
1 min read
turtle.setpos() and turtle.goto() functions 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.setpos() This method is used to move the turtle to an absolute position. This method has Aliases: setpos, setposition, goto. S
1 min read
Turtle Race Game Using Python - Turtle Graphics Library
Turtle graphics is a popular way to introduce programming concepts to beginners. It's a fun and interactive module in Python that lets you create simple drawings and animations using a "turtle" that moves around the screen. In this tutorial, we'll create an exciting turtle race game where you can bet on the color of a turtle and watch as they race
4 min read
turtle.stamp() 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.stamp() This method is used to stamp a copy of the turtleshape onto the canvas and return its id. It doesn't require any argum
1 min read
turtle.Screen().bgcolor() 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.Screen().bgcolor() This method is used to set or return background color of the Turtle Screen. Syntax: turtle.bgcolor(*args) P
1 min read
turtle.distance() 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.distance() This method is used to return the distance from the turtle to (x,y) in turtle step units. Syntax : turtle.distance(
2 min read
turtle.hideturtle() 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.hideturtle() This method is used to make the turtle invisible. It's a good idea to do this while you're in the middle of a com
1 min read
turtle.title() 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.title() This function is used to set the title of turtle-window. It requires only one argument as "titlestring" a string, to a
1 min read
turtle.width() 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.width() This method is used to set or return the line thickness. Set the line thickness to width or return it. If resizemode i
2 min read
turtle.shape() 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.shape() This function is used to set the turtle shape to shape with a given name or, if the name is not given, return the name
1 min read
turtle.write() 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.write() This function is used to write text at the current turtle position. Syntax : turtle.write(arg, move=False, align='left
1 min read
turtle.isdown() 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.isdown() This method is used to check whether the turtle is down or not. It doesn't require any argument and return a boolean
1 min read
turtle.speed() 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.speed() The turtle.speed() method is used to change the speed of the turtle by the value of the argument that it takes. Return
1 min read
turtle.clone() 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.clone() The turtle.clone() method is used to create and return a clone of the turtle with the same position, heading, and turt
2 min read
turtle.isvisible() 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.isvisible() The turtle.isvisible() method is used to return True if the Turtle is shown, False if it's hidden. This method doe
1 min read
turtle.setx() 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.setx() This method is used to set the turtle's first coordinate to x, leave the second coordinate unchanged. Here, whatever th
1 min read
turtle.sety() 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.sety() This method is used to set the turtle's second coordinate to y, leaving the first coordinate unchanged. Here, whatever
1 min read
turtle.seth() 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.seth() This method is used to set the orientation of the turtle to to_angle. This method requires only one argument as an angl
2 min read
turtle.home() 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.home() This function is used to move the turtle to the origin i.e. coordinates (0,0). Whatever, the position of the turtle is,
2 min read
turtle.xcor() 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.xcor() This function is used to return the turtle's x coordinate of the current position of turtle. It doesn't require any arg
1 min read
turtle.ycor() 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.ycor() This function is used to return the turtle's y coordinate of the current position of turtle. It doesn't require any arg
1 min read
turtle.getscreen() 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.getscreen() This function is used to Return the TurtleScreen object, the turtle is drawing on. It doesn't require any argument
1 min read
turtle.getpen() 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.getpen() This function is used to return the Turtleobject itself. It doesn't require any argument. Syntax : turtle.getpen() Be
1 min read
turtle.getshapes() 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.getshapes() This function is used to return a list of names of all currently available turtle shapes. It doesn't require any a
1 min read
turtle.numinput() 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.numinput() This function is used to pop up a dialog window for the input of a number. The number input must be in the range mi
1 min read
turtle.ondrag() 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.ondrag() This function is used to bind fun to mouse-move event on this turtle on canvas. Syntax : turtle.ondrag(fun, btn, add)
1 min read
turtle.Screen().turtles() 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.Screen().turtles() This function is used to return the list of turtles on the screen. This doesn't require any argument. Synta
1 min read
turtle.window_height() 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.window_height() This function is used to return the height of the turtle window. It doesn't require any argument. Syntax : tur
1 min read
turtle.window_width() 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.window_width() This function is used to return the width of the turtle window. It doesn't require any argument. Syntax : turtl
1 min read
turtle.onkey() 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.onkey() This function is used to bind fun to the key-release event of the key. In order to be able to register key-events, Tur
1 min read
Article Tags :
Practice Tags :
  翻译: