- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to bind multiple events with one "bind" in Tkinter?
For a particular application, if we want to perform multiple tasks with the help of buttons defined in it, then we can use the bind(Button, callback) method which binds the button and the event together to schedule the running of the event in the application.
Let us suppose we want to bind multiple events or callback with a single <bind>, then we have to first iterate over all the widgets to get it as one entity. The entity can be now configurable to bind the multiple widgets in the application.
Example
# Import the required libraries from tkinter import * from tkinter import ttk # Create an instance of tkinter frame or window win = Tk() # Set the size of the window win.geometry("700x350") def change_bgcolor(e): label.config(background="#adad12") def change_fgcolor(e): label.config(foreground="white") # Add a Label widget label = Label(win, text="Hello World! Welcome to Tutorialspoint", font=('Georgia 19 italic')) label.pack(pady=30) # Add Buttons to trigger the event b1 = ttk.Button(win, text="Button-1") b1.pack() # Bind the events for b in [b1]: b.bind("<Enter>", change_bgcolor) b.bind("<Leave>", change_fgcolor) win.mainloop()
Output
If we run the above code, it will display a window that contains a button.
When we hover over the button, it will change the background color of the Label. Leaving the button will change the font color of the Label widget.
- Related Articles
- How to bind events to Tkinter Canvas items?
- How to bind jQuery events on elements generated through other events?
- How to bind to shift+tab in Tkinter?
- How to bind events on dynamically created elements using jQuery?
- How to bind all the number keys in Tkinter?
- How to bind a key to a button in Tkinter?
- How to bind jQuery events within dynamic content returned via Ajax?
- How to bind the Enter key to a tkinter window?
- How to bind a click event to a Canvas in Tkinter?
- What are bind variables? How to execute a query with bind variables using JDBC?
- How to bind the Escape key to close a window in Tkinter?
- How to bind the spacebar key to a certain method in Tkinter?
- How do I bind the enter key to a function in Tkinter?
- How can I bind all events on a DOM element using jQuery?
- How to bind multiple versions of a DB2 program into a package?

Advertisements