
Event Handling In Tkinter Python means to bind the keyboard or mouse buttons to the Tkinter and use them to call some functions.
We use bind() function to bind keys and functions to each other.
Here, functions can be in-built or made by the programmer.
Let’s see the code to call the in-built function and self made function using the gui.
Code
# importing tkinterfrom tkinter import *# initializing rootroot = Tk()# width and height variablescan_width = 300can_height = 200# setting geometry of guiroot.geometry(f"{can_width}x{can_height}")# setting title of guiroot.title("Event handling")# creating a labelLabel(root, text="Click anywhere").pack()# initializing variablei = 0# defining a functiondef me(event):global iLabel(root, text=f"You clicked me {i} time").pack()Label(root, text="Double click to exit screen").pack()i=i+1# binding mouse left button to root and calling me functionroot.bind("<Button-1>", me)# binding double click mouse left button to root and calling# in-built quit functionroot.bind("<Double-1>", quit)# calling mainlooproot.mainloop()
Output
Now, here is the code to call the previous functions using the button.
Code
# importing tkinterfrom tkinter import *# initializing rootroot = Tk()# width and height variablescan_width = 300can_height = 200# setting geometry of guiroot.geometry(f"{can_width}x{can_height}")# setting title of guiroot.title("Event handling")# creating a buttonwin = Button(root, text="Click me")# packing the button to gui to show it on screenwin.pack()# initializing variablei = 0# defining a functiondef me(event):global iLabel(root, text=f"You clicked me {i} time").pack()Label(root, text="Double click to exit screen").pack()i=i+1# binding mouse left button to win and calling me functionwin.bind("<Button-1>", me)# binding double click mouse left button to win and calling# in-built quit functionwin.bind("<Double-1>", quit)# calling mainlooproot.mainloop()
Output
Also Read:
- Struggling to Generate Ghibli-Style AI Images? Here’s the Real Working Tool That Others Won’t Tell You About!In recent weeks, the digital art community has been captivated by a burgeoning trend: the creation of Studio Ghibli-style images using artificial intelligence (AI). This…
- ChatGPT vs DeepSeek: Who is the winner?Here’s a detailed comparison between ChatGPT (specifically GPT-4o) and DeepSeek-V3 based on various online resources, focusing on key metrics such as time to build, cost,…
- People are becoming AI Engineer with this free course in 2025: Here is how to join this…Artificial Intelligence (AI) has become a cornerstone of technological advancement, shaping industries and transforming careers. If you’ve been looking to upskill or dive into the…
- Apply to Google’s Student Training in Engineering Program (STEP) Intern, 2025Google’s Student Training in Engineering Program (STEP) Intern, 2025, is a fantastic opportunity for students passionate about programming and software development. Designed to support skill-building…
- Self-Driving Car Saves Falling Pedestrian, Showcases Promise of Autonomous TechnologyIn a dramatic demonstration of the potential for self-driving technology to enhance road safety, a Waymo’s autonomous vehicle recently avoided a serious accident when a…