Table of Contents
The turtle performs some actions when the user hits certain keys on the keyboard or when the user clicks/moves the mouse. Such actions performed when clicking a mouse or pressing the keys on a keyboard are called events in python turtle. Events can be performed in two ways:
- Mouse event – action triggered when a mouse button is clicked.
- Keyboard event – action triggered when a key from the keyboard is pressed.
An action triggered due to clicks on the mouse button is called a mouse event. We can perform a mouse event in four ways:
Before we look into all the four methods of mouse events, let us talk about the parameters the method takes in. All four methods accept three parameters as listed below:
- fun – a function with two arguments which will be called with the coordinates of the clicked point on the canvas.
- btn – number of a mouse button, default is 1 (i.e, left button).
- Mouse button 1 – left button
- Mouse button 2 – middle button
- Mouse button 3 – right button
- add – True or False ( if True, a new binding will be added, otherwise, it will replace a former binding )
No need to worry if you are not clear about the parameters. You will understand more when the methods are explained with an example. Now, let us look into all four methods of mouse events with an example.
A Turtle performs an action when the user clicks a mouse on the turtle.
onclick(fun, btn=1, add=None)
by default, btn will be 1(i.e. left click) and then add as None.
from turtle import * pensize(5) shape("turtle") def square(x, y): for i in range(4): forward(200) left(90) onclick(square)
Explanation: We have declared a function called square which will draw us a square of side 200px. Inside the function, two parameters have been passed i.e x and y. This is because as per the first parameter of the method(fun), a function with two arguments(x, y) will be called with the coordinates of the clicked point on the canvas. At last, when we call a function, the function has been called by onclick() method. Whereby, the function will be executed only when the left button on a mouse is clicked on a turtle. We are not mentioning a mouse button explicitly because by default left button will be taken in as an argument for btn parameter.
The turtle performs an action when the user clicks on the canvas.
onscreenclick(fun, btn=1, add=None)
The difference between onclick() and onscreenclick() method is that the action will be triggered (to draw a square) when you left-click on any canvas. In onscreenclick(), it need not have to be particularly on the turtle.
from turtle import * pensize(5) shape("turtle") def square(x, y): for i in range(4): forward(200) left(90) onscreenclick(square)
In this method, Turtle performs an action only when the clicked mouse is released from the turtle.
onrelease(fun, btn = 1, add=True)
by default, btn will be left-clicked and add as True
To make a clear understanding of the onrelease() method, let us try with the same example used on onclick() method. The only difference will be that the square will be drawn when you release a left-click from the turtle.
from turtle import * pensize(5) shape("turtle") def square(x, y): for i in range(4): forward(200) left(90) onrelease(square)
The turtle performs an action when the user drags the turtle.
ondrag (fun, btn=1, add=None)
by default, btn will be left-clicked and add as None
from turtle import * shape("turtle") color("blue") shapesize(2) speed(10) pensize(3) def draw(x,y): # stop backtracking ondrag(None) # drawing as per the x and y # coordinates of mouse movement goto(x, y) # Call fun again ondrag(draw) ondrag(draw) # take screen in mainloop mainloop()
In the above example, we have used mainloop() which must be the last statement in a turtle graphics program. It is used to call Tkinter main loop function.
Action will be triggered when a user presses a key on a keyboard. And it is called keyboard events.
Note: A listen() method with dummy arguments from the turtle module is used along with the keyboard events method to set focus on the Turtle screen.
All the keyboard events methods accept two parameters mentioned below:
- fun – the name of a function with no argument
- key – a name of the key. It should be string data type such as “a”, “b”, etc… or if you want to use key-symbols then metion the key name as a string such as “space”, “up”, “down”, “left”, etc…
1. onkey() | onkeyrelease()
The onkey() and onkeyrelease() method calls the user-defined function and performs the action defined inside the function when the specified key is pressed. Both the parameters are required in this method.
onkey(fun, key) || onkeyrelease(fun, key)
Example (onkey() method):
from turtle import * pensize(5) shape("turtle") def cir(): circle(100) def square(): for i in range(4): forward(150) left(90) onkey(cir, "c") onkey(square, "s") listen()
The onkeypress() method performs a similar action as the onkey() method, the only difference between the two methods is that the second parameter in onkeypress() method is optional whereas it is mandatory in onkey() method. If the key is not mentioned explicitly in onkeypress() method, any key will be used to trigger an action.
from turtle import * pensize(5) shape("turtle") def cir(): circle(100) onkeypress(cir) listen()
Events in python turtle are used to trigger some actions and it’s used in the development of games and drawings.