Hello readers, in this article let’s try to understand what events are in Tkinter. Before starting with this topic, we must remember that Python’s Tkinter package is used to design GUI-based interfaces. Tkinter has a variety of built-in functionalities and extensions that can be used to enhance the functionality and performance of the application.
What is an event?
The basic definition of the word event is the occurrence of a particular thing or could be defined as a thing that happens or takes place similarly in Tkinter events are operations carried out by the user such as mouse movement or key press. Events are used to connect the actions performed by the user to a logic understandable by the application which results in its graphical changes.
When we use widgets in Tkinter, they do have a lot of built-in behaviours. For instance, a button will execute its command callback in response to a mouse click. However, you can add, modify, or remove actions/behaviour using Tkinter’s event-binding functionality.
This binding functionality is achieved by using
.bind() function and the event along with the event handler is defined in the widget
widget.bind(event, event handler)
- Event – occurrence caused by the user that might reflect changes.
- Event Handler – function in your application that gets invoked when the event takes place.
- Bind – configuring an event handler (python function) that is called when an event occurs to a widget.
As an output first a dialogue box will appear with a button displayed on it, once the user clicks the left button on its mouse on the displayed button, the event handler (here the function named
askPython acts as an event handler) is invoked and the text message “You are successfully learning Tkinter” will appear in the output window.
Syntax of the events.
An event sequence is typically a string that contains one or more event patterns. Each event pattern depicts a single possible outcome. If a sequence has many event patterns, the handler won’t be called until every pattern occurs in that sequence. Remember event sequence is always described between “
Syntax: <modifier - type - detail>
The modifier and detail parts are optional. The type is the crucial part of defining the event.
- modifier – section to specify combinations, such as pressing and holding the shift or control buttons when using the mouse or clicking other keys.
- type – defines a specific kind of event, like a mouse click or key press.
- detail – elements to use when describing the mouse button or key you’re seeking for. This is 1 for button 1, 2 for button 2, or 3 for button 3 on a mouse.
Types of events
One of the mouse buttons was clicked by the user. Which button is specified in the detail section Example: <button-1> is the leftmost button, <button-2> is the middle button(if available), <button-3> the rightmost button, <button-4> for scroll up and <button-5 > for scroll down.
The mouse button was released by the user. Since the user may move the mouse off the widget to prevent mistakenly pressing the button, this is undoubtedly a better alternative in most scenarios than the Button event.
The user modified the size of a widget, for example by extending a window’s corner or side.
A widget is being destroyed.
The user placed the mouse pointer over a widget’s visible area. (This is distinct from the enter key, which is a KeyPress event for a key)
This happens whenever at least a portion of your widget or application becomes visible after being hidden by another window.
This may occur in response to user interaction (such as using the tab key to switch across widgets) or programmatically (for instance, when your software invokes the .focus set() method on a widget).
The widget that was the input focus was changed. Similar to <FocusIn>, either the user or your software can initiate this event.
The user pressed a keyboard key. This key is specified in the detail section of the event section. The word “key” can be used as an abbreviation.
The user released the key.
The user moved the mouse pointer out of a widget.
An application is mapping, or making a widget visible. For instance, this will take place if you use the widget’s.grid() method.
When a mouse button is depressed, the mouse is moved. Use <B1-Motion>, <B2-Motion> or <B3-Motion> to select the left, middle, or right mouse button, respectively.
The user moved the mouse wheel up or down.
Unmapped widgets are those that are no longer visible. For instance, when you use the widgets.grid remove() method, this occurs.
When at least a portion of the application window is visible on the screen, this occurs.
We have discussed in detail the topic events in Tkinter and understood that the primary aim of using events is to comprehend the actions performed by the user, (it can be via mouse movements or key press) and to respond to these by a change in behaviour of the GUI.