import tkinter as tk root = tk.Tk() # create a label widget my_label = tk.Label(root, text="Hover over me!") # bind events to label widget my_label.bind("", lambda e: my_label.config(bg="gray")) my_label.bind(" ", lambda e: my_label.config(bg="white")) my_label.pack() root.mainloop()
import tkinter as tk from tkinter import messagebox root = tk.Tk() # create a label widget my_label = tk.Label(root, text="Click me to show a message box!") # bind event to label widget my_label.bind("In this example, the lambda function displays a message box when the user clicks on the label widget. The message box shows a message "You clicked the label!".", lambda e: messagebox.showinfo("Message", "You clicked the label!")) my_label.pack() root.mainloop()