In this tutorial, I will explain how to master Python Tkinter events in detail with real-world examples. One of my team members asked me about Tkinter events which made me explore more about this topic and I will share my experiences and provide a step-by-step guide to help you master Tkinter event handling.
Python Tkinter Events
In Tkinter, events are actions that occur when a user interacts with the GUI, such as pressing a key, clicking a mouse button, or resizing a window. Tkinter provides a powerful mechanism to handle these events and make your application responsive.
Read How to Create Tabbed Interfaces in Python with Tkinter Notebook Widget?
Types of Tkinter Events
Here are some common Tkinter events that you will frequently encounter:
- Button Click Event (
<Button-1>) - Key Press Event (
<Key>) - Mouse Motion Event (
<Motion>) - Window Resize Event (
<Configure>) - Focus In/Out Events (
<FocusIn>,<FocusOut>)
Master Python Tkinter Events
Let us see some different events that can be handled in Python Tkinter.
Check out How to Create an On/Off Toggle Switch in Python Tkinter?
1. Bind Events to Widgets
To handle events in Tkinter, you need to bind them to specific widgets. The bind() method is used to associate an event with a widget and specify the function or method to be called when the event occurs.
Here’s an example of binding a button click event to a function:
import tkinter as tk
def handle_click(event):
print("Button clicked!")
window = tk.Tk()
button = tk.Button(window, text="Click Me")
button.bind("<Button-1>", handle_click)
button.pack()
window.mainloop()In this example, we define a function handle_click() that will be called whenever the button is clicked. We use the bind() method to associate the <Button-1> event (left mouse button click) with the button widget and specify handle_click as the callback function.
You can see the output in the screenshot below.

Read How to Validate User Input in Python Tkinter?
2. Display Text on Hover
In Tkinter, event binding allows you to associate specific user interactions, such as mouse movements or key presses, with functions. The following example demonstrates how to display text when the mouse hovers over the window using the <Enter> event.
from tkinter import *
ws = Tk()
ws.title("Python Guides")
ws.geometry("700x350")
def Show_Text(e):
label.config(text="Life is short/ Do what you love", font=('Helvetica', 14, 'bold'))
label = Label(ws, text="")
label.pack(pady=50)
# Bind the event to mouse hover instead of mouse click
ws.bind('<Enter>', Show_Text)
ws.mainloop()You can see the output in the screenshot below.

In starting there is no text on the widget when we move the cursor over the widget we get some text “Life is short/Do what you love”.
3. Event Widget
The widget is a component of the Graphical user interface which shows the information from which the user can interact with the operating system. The event occurs in the widget.
from tkinter import *
def clickI(event):
print ("you clicked on"), event.widget
event.widget.config(text="Thank_You")
def clickII(event, Obj):
print ("you clicked on"), Obj
Obj.config(text="Thank_You!")
ws = Tk()
ws.geometry("200x200")
a1 = Label(ws, text="Press")
a2 = Label(ws, text="No, Press!")
a1.pack(pady=10)
a2.pack(pady=10)
a1.bind("<1>", clickI)
a2.bind("<1>", lambda event, obj=a2: clickII(event, obj))
ws.mainloop()You can see the output in the screenshot below.

we can see we created a widget and bound two buttons that perform some action while clicking on it.
Check out How to Create a Search Box with Autocomplete in Python Tkinter?
4. Event Attributes
Event Attributes in Tkinter allow you to access specific details about the event that triggered an action, such as the key pressed or the mouse button clicked. This example demonstrates how to capture and display event-specific information, such as key types and mouse button actions, using event attributes.
import tkinter
from tkinter import *
ws = Tk( )
ws.geometry("200x200")
display='Press Any Button, or Press Key'
Lab= Label(ws, text=display, width=len(display))
Lab.pack(pady=40)
def key(eve):
if eve.char==eve.keysym:
message ='Normal Key %r' % eve.char
elif len(eve.char)==1:
message ='Punctuation Key %r (%r)' % (eve.keysym, eve.char)
else:
message ='Special Key %r' % eve.keysym
Lab.config(text=message)
Lab.bind_all('<Key>', key)
def do_mouse(eventname):
def mouse_binding(event):
message = 'Mouse event %s' % eventname
Lab.config(text=message)
Lab.bind_all('<%s>'%eventname, mouse_binding)
for i in range(1,4):
do_mouse('Button-%s'%i)
do_mouse('ButtonRelease-%s'%i)
do_mouse('Double-Button-%s'%i)
ws.mainloop( )You can see the output in the screenshot below.

We can see how attributes are working on the console as we use the mouse pointer attribute which shows a mouse event on a screen.
Read How to Create Message Boxes with Python Tkinter?
5. event_generate
In the event_generate, we generated an event that shows some action and brings something into existence.event_generate is a default process that calls back the event.
import threading
import time
import queue
from tkinter import *
ws = Tk()
ws.geometry("200x200")
comque= queue.Queue()
def timeThread():
prestime = 0
while 1:
comque.put(prestime)
try:
ws.event_generate('<<TimeChanged>>', when='tail')
except TclError:
break
time.sleep(1)
prestime += 1
clcvar = IntVar()
Label(ws, textvariable=clcvar, width=9).pack(pady=10)
def timeChanged(event):
clcvar.set(comque.get())
ws.bind('<<TimeChanged>>', timeChanged)
Thr=threading.Thread(target=timeThread)
Thr.start()
ws.mainloop()You can see the output in the screenshot below.

In this output, time is generated which continuously changes in seconds.
Read How to Save Text to a File Using Python Tkinter?
Advanced Event Handling Techniques
Let us see some important advanced event-handling techniques.
1. Event Objects and Attributes
When an event occurs, Tkinter creates an event object that contains information about the event. General event callbacks must accept one argument, event which is a Tkinter event object. This event object has several attributes that provide details about the event.
Here are some commonly used event object attributes:
event.widget: The widget that triggered the event.event.xandevent.y: The x and y coordinates of the mouse pointer relative to the widget.event.char: The character associated with a key press event.event.keysym: The symbolic name of the key pressed.
You can access these attributes within your event-handling functions to perform specific actions based on the event details.
Check out How to Cancel Scheduled Functions with after_cancel()
2. Bind Events to Canvas Elements
Tkinter’s Canvas widget allows you to create graphical elements like lines, rectangles, and ovals. You can also bind events to these canvas elements to make them interactive.
Here’s an example of binding events to canvas elements:
import tkinter as tk
def handle_click(event):
print(f"Clicked at coordinates: ({event.x}, {event.y})")
window = tk.Tk()
canvas = tk.Canvas(window, width=400, height=300)
canvas.pack()
rectangle = canvas.create_rectangle(50, 50, 150, 100, fill="blue")
canvas.tag_bind(rectangle, "<Button-1>", handle_click)
window.mainloop()In this example, we create a canvas and a blue rectangle on it. We use the tag_bind() method to bind the click event to the rectangle. Whenever the rectangle is clicked, the handle_click() function is called, and it prints the coordinates of the click event.
Best Practices for Tkinter Event Handling
- Use meaningful function names: Choose descriptive names for your event-handling functions to improve code readability.
- Keep event handlers concise: Avoid putting too much logic inside event handlers. Instead, extract complex operations into separate functions.
- Use lambda functions for simple callbacks: If your event handler is a single line of code, you can use lambda functions for brevity.
- Unbind events when no longer needed: If you dynamically bind events to widgets, remember to unbind them when they are no longer required to avoid memory leaks.
- Handle exceptions gracefully: Wrap your event handling code in try-except blocks to catch and handle exceptions gracefully.
Read How to Create GUI Layouts with Python Tkinter Separator?
Conclusion
In this tutorial, I have explained how to master Python Tkinter events. I discussed the types of Tkinter events and how to master Python Tkinter events with examples and screenshots. I also covered some advanced event handling techniques.
You may like to read:
- How to Add Functions to Python Tkinter?
- How to Create a Filter() Function in Python Tkinter?
- How to Create a Python Tkinter Panel with Search Functionality?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.