Tkinter Events are very useful for any application where we need to perform a certain task or action. In Tkinter, the events are generally created by defining the function which contains the piece of code and the logic for the certain event. To call the event, we generally bind the Event with some Keys or a Button widget. The bind function takes two parameters ('<Key-Combination>', callback) which enable the button to trigger the event.
Using the same approach in the following example, we will trigger a popup message by pressing the key combination <Shift + Tab>.
# Import the required libraries from tkinter import * from tkinter import messagebox # Create an instance of tkinter frame win= Tk() # Set the size of the tkinter window win.geometry("700x350") # Define a function to show the popup message def show_msg(e): messagebox.showinfo("Message","Hey There! I hope you are doing well.") # Add an optional Label widget Label(win, text = "Admin Has Sent You a Message. " "Press <Shift+Tab> to View the Message.", font = ('Aerial 15')).pack(pady= 40) # Bind the Shift+Tab key with the event win.bind('<Shift-Tab>', lambda e: show_msg(e)) win.mainloop()
When we execute the above program, it will display a window containing a label widget. When we press the key combination <Shift + Tab>, it will pop up a message on the screen.