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 tkinter from tkinter import * # initializing root root = Tk() # width and height variables can_width = 300 can_height = 200 # setting geometry of gui root.geometry(f"{can_width}x{can_height}") # setting title of gui root.title("Event handling") # creating a label Label(root, text="Click anywhere").pack() # initializing variable i = 0 # defining a function def me(event): global i Label(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 function root.bind("<Button-1>", me) # binding double click mouse left button to root and calling # in-built quit function root.bind("<Double-1>", quit) # calling mainloop root.mainloop()
Output
Now, here is the code to call the previous functions using the button.
Code
# importing tkinter from tkinter import * # initializing root root = Tk() # width and height variables can_width = 300 can_height = 200 # setting geometry of gui root.geometry(f"{can_width}x{can_height}") # setting title of gui root.title("Event handling") # creating a button win = Button(root, text="Click me") # packing the button to gui to show it on screen win.pack() # initializing variable i = 0 # defining a function def me(event): global i Label(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 function win.bind("<Button-1>", me) # binding double click mouse left button to win and calling # in-built quit function win.bind("<Double-1>", quit) # calling mainloop root.mainloop()
Output
Also Read:
- Simple Code to compare Speed of Python, Java, and C++?Introduction There’s often a lot of debate about the speed of different programming languages, with many assuming that C++ is always the fastest and Python…
- Falling Stars Animation on Python.Hub October 2024Hello Python enthusiasts! Check out the cool animation below, featured on our Instagram page, python.hub. You can view the latest reel, posted on October 8,…
- Most Underrated Database Trick | Life-Saving SQL CommandHello folks! Today we are again back with a super important article on the Most underrated SQL & Database Trick to save your entire application….
- Python List MethodsHello friends, in this article, we will explore various Python List methods, indispensable tools in a programmer’s toolkit for manipulating lists efficiently. List methods in…
- Top 5 Free HTML Resume Templates in 2024 | With Source CodeIntroduction Hello friends! Welcome to another article where I will share more useful resources for free. Today, I will share the Best 5 HTML resume…