In programming, an event is something that occurs within an application's environment such as a mouse click, key press or changes in the GUI. In Python, using the Tkinter GUI package, we can handle these events by binding them to either predefined or user-defined functions, this allows us to run pieces of code when a certain event occurs within a widget.
Basic Tkinter event binding syntax
To bind an event with a function, we can make use of the bind() function that is included in all widgets. The bind() function takes 2 arguments:
-
event
: A representative string that contains details about which event to listen for, this must be given in the following format:
"<modifier-type-detail>"
. The only required part of this string is the "type" section, this represents the type of event to listen for, if you leave out the other section it can be written as "<type>".
-
handler
: The name of the function to call when the event occurs. This is only the name of the function, meaning you cannot include a list of your own arguments.
The function included as the event handler will be passed an event object which includes details about the event that was triggered, meaning you should include a parameter to be assigned this object in your function.
As an example, let's assign an event handler to a mouse click event on a label that will print a message when activated:
from tkinter import *
window = Tk()
def mouseClick( event ):
print( "mouse clicked" )
label = Label( window, text="Click me" )
label.pack()
label.bind( "<Button>", mouseClick )
window.mainloop()
The above code will create a window with a single label with the text "click me", when any of the mouse buttons are clicked (left click, right click or middle mouse button click) the mouseClick() function will be called and will print "mouse clicked" to the console.
Event types
There are several event types available with Tkinter, including:
KeyPress
: Activated when a keyboard button has been pressed, the
Key
event can also be used for this.
KeyRelease
: Activated when a keyboard button is released.
Button
: Activated when a mouse button has been clicked.
ButtonRelease
: Activated when a mouse button has been released.
Motion
: Activated when the mouse cursor moves across the designated widget.
Enter
: Activated when the mouse cursor enters the designated widget.
Leave
: Activated when the mouse cursor leaves the designated widget.
MouseWheel
: Activated when the mouse wheel is scrolled.
FocusIn
: Activated when the designated widget gains focus through user input such as the mouse clicking on it.
FocusOut
: Activated when the designated widget loses focus.
Configure
: Activated when the designated widget's configurations have changes such as its width being adjusted by the user or its border being adjusted.
Event modifiers
An event modifier can alter the circumstances in which an event's handler is activated, for example, some modifiers will require another button to be depressed while the event occurs.
Control
: Requires that the control button is being pressed while the event is occurring.
Alt
: Requires that the alt button is being pressed while the event is occurring.
Shift
: Requires that the shift button is being pressed while the event is occurring.
Lock
: Requires that caps lock is activated when the event occurs.
Double
: Requires that the given event happens twice in quick succession (such as a double click)
Triple
: Requires that the given event happens three times in quick succession
Quadruple
: Requires that the given event happens four times in quick succession
As an example, let's create an event handler that only activates on a double click:
label.bind( "<Double-Button>", mouseClick )
Event details
The detail section of the event string allows us to specify a more specific event such as only a certain key on the keyboard being pressed or only a certain mouse being being pressed.
-When using
Button
or
ButtonRelease
we can give a numeric detail from
1 to 5
which represents the specific mouse button you wish to have the handler trigger from.
-When using
Key
,
KeyPress
or
KeyRelease
we can give the
ASCII value
of the specific key we wish to trigger the event.
As an example, let's create an event handler that only activates on the double click of the left mouse button.
label.bind( "<Double-Button-1>", mouseClick )
The event object
The event object that is passed to the handler when the event is triggered can be used to collect and use information about the event that has occurred. The event object has a number of useful properties such as:
-
keysym
: Returns the name of the key (space, e, return) that triggered a keyboard based event such as
KeyPress
,
Key
or
KeyRelease
.
-
keycode
: Returns the code of the key that triggered a keyboard based event.
-
button
: Returns the mouse button (1-5) that triggered a mouse based event.
-
x
: Returns the x coordinate of where events such as
Button
occur.
-
y
: Returns the y coordinate of where events such as
Button
occur.
-
width
: Returns the current width of the widget associated with the event.
-
height
: Returns the current height of the widget associated with the event.
As an example, let's create an event handler that prints the x and y coordinates of a mouse click event to the console.
def mouseClick( event ):
print( "mouse clicked at x=" + event.x + " y=" + event.y )
label.bind( "<Button>", mouseClick )
Potential output of the code could be:
"mouse clicked at x=45 y=23"