Event Handling In Tkinter Python

Event Handling In Tkinter Python

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:


  • Python 3.12.1 is Now Available
    Thursday, December 7, 2023 Python enthusiasts, rejoice! Python 3.12.1, the first maintenance release of Python 3.12, is now available for download here. This update packs…
  • Best Deepfake Apps and Websites You Can Try for Fun
    Deepfake is one technology that remains consistently impressive. Apps like Lensa AI have taken the internet by storm. It lets you create deepfake portraits within…
  • Amazon launched free Prompt Engineering course: Enroll Now
    Introduction In this course, you will learn the principles, techniques, and the best practices for designing effective prompts. This course introduces the basics of prompt…
  • 10 GitHub Repositories to Master Machine Learning
    1. ML-For-Beginners by Microsoft2. ML-YouTube-Courses3. Mathematics For Machine Learning4. MIT Deep Learning Book5. Machine Learning ZoomCamp6. Machine Learning Tutorials7. Awesome Machine Learning8. VIP Cheat Sheets…
  • Hello World in 35 Programming Languages
    A timeless custom known as the “Hello, World!” program marks the start of every programmer’s adventure in the wide world of programming. This surprisingly easy…


Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you