Python turtle onclick with examples

In this Python turtle tutorial, we will learn about Python turtle onclick and we will also cover different examples related to turtle onclick. And, we will cover these topics.

  • Python turtle onclick
  • Python turtle onclick position
  • Python turtle onclick exit

Python turtle onclick

In this section, we will learn about turtle onclick in Python turtle.

The onclick function gets evaluated when we click on the screen. It enables the user to perform an action. When the user clicks on-screen the action is performed and the object starts working.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle. The turtle() method is used to make objects.

  • tur.speed(1) is used to give the slowest speed to the turtle.
  • tur.forward(100) is used to move the turtle in the forward direction.
  • tur.onclick(func) allows the user to click for some action.
from turtle import *

import turtle as tur 
  

def func(i,j):
      

    tur.right(90)
    tur.forward(100)
  
tur.speed(1)
  
tur.forward(100)
 
tur.onclick(func)
tur.done()

Output:

After running the above code we get the following output in which we can see onclick on arrow turtle move in forwarding direction.

Python turtle onclick
Python turtle onclick Output

Python turtle onclick position

In this section, we will learn about how to get a position by simply onclick on-screen in Python turtle.

The onclick function gets evaluated when we click onscreen. The user simply clicks on the screen and the position of the turtle is printed on the command prompt.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle as tur from this we execute the onclick function.

tur.onclick(getPosition) is used to get the position by simply onclick on the screen.

from turtle import *
import turtle as tur
def getPosition(i,j):
    print("(", i, "," ,j,")")
    return

def mainscr():
    tur.onclick(getPosition)
    tur.mainloop()
mainscr()

Output:

After the above code, we get the following output in which we can see the position of the turtle is shown on the command prompt by simply clicking on the screen.

Python turtle onclick position
Python turtle onclick position Output

Read How to Create a Snake game in Python using Turtle

Python turtle onclick exit

In this section, we will learn about how to exit simply onclick on-screen in Python turtle.

The onclick() function gets evaluated when we click onscreen. Similarly, the onclickexit() function is used after the execution of the program by just simply clicking on the screen. After clicking on the screen the screen gets terminated.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle as tur. The turtle() method is used to make objects.

tur.circle(35) is used for drawing the circle on the screen.

tur.right(115) is used to move the turtle in the right direction.

tur.exitonclick() function is used to exit from the screen if and only if the mouse is clicked.

from turtle import *

import turtle as tur

for x in range(3):
  tur.circle(35)
  tur.right(115)
  
# exit from the screen 
# if and only if
# mouse is clicked
tur.exitonclick()

Output:

After running the above code we will get the following output in which we see the circle is drawn on the screen after the completion of the program onclickexit() function execute if and only if the mouse is clicked on the screen.

Python turtle onclickexit
Python turtle onclickexit Output

Check out the below Python Turtle tutorials:

So, in this tutorial, we discussed Python turtle onclick and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Python turtle onclick
  • Python turtle onclick position
  • Python turtle onclick exit