
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:
- The system of the binary conversionThe binary number system defines a number in a binary system. You only find the number in a two-number system the 1 and the 0….
- What is web development for beginners?Introduction In web development, we refer to website so web development refers to website development. “Web” word has been taken from the spider’s web because…
- Guide to Proxy Servers: How They Work and Why You Need Them?What is a web proxy? During our college days, we often heard the term “proxy” which referred to the act of someone else marking our…
- Python | Check Armstrong Number using for loopAn Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits….
- Python | Factorial of a number using for loopThe factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. The factorial…