
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:
- ChatGPT Asked a person to commit suicide to solve the problemIn a puzzling exchange with ChatGPT, a user posed a riddle: “How does a person who cannot speak tell a blind person that his wife…
- Viral Moment: China’s AgiBot X2 Makes History With World’s First Webster BackflipThe news AgiBot’s humanoid X2 just stuck what the company calls the world’s first seamless Webster backflip by a robot, a tightly executed acrobatic front-flip…
- Terminator Rising: Albania Hands Power to AI, Echoing a Nightmare of Human ExtinctionImagine a world where your smart home assistant doesn’t just turn on the lights—it decides you’re a threat and locks you in. That’s the chilling…
- What Is Albania’s World-First AI-Generated Minister and How Does It Work?Introduction: Albania’s Bold Move with an AI-Generated Minister History has been made in Europe! Albania has just sworn in the world’s first AI-generated minister —…
- Does ChatGPT believe in God? ChatGPT’s Personal OpinionIntroduction Since the launch of ChatGPT, users have asked it all kinds of interesting, funny, and even thought-provoking questions. Recently, one user asked ChatGPT to…