Tkinter Binding multiple event handlers to single event
Previously the program could bind an event handler when using the bind( ) method. tkinter also allows us to perform an event binding multiple event handlers, again using the bind( ) method, but the newly added event handler needs to add the parameter add="+"
inside the bind( ) method.
from tkinter import *
def buttonClicked1(): # Button Event Handler 1
print("#1 Command event handler, I like tkinter...")
def buttonClicked2(event): # Button Event Handler 2
print("#2 Bind event handler, I like tkinter...")
def buttonClicked3(event): # Button Event Handler 3
print("#3 Bind event handler, I like tkinter...")
def buttonClicked4(event): # Button Event Handler 4
print("#4 Bind event handler, I like tkinter...")
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
btn = Button(root,text="tkinter",command=buttonClicked1)
btn.pack(anchor=W,padx=10,pady=10)
btn.bind("<Button-1>",buttonClicked2,add="+")
btn.bind("<Button-1>",buttonClicked3,add="+")
btn.bind("<Button-1>",buttonClicked4,add="+")
root.mainloop()
Output:
If you click the tkinter button, you can see the results of executing the two event handlers in the Python Shell window.
From the above we also found that when the click button event occurs, the program will first execute the program bound by bind() and then execute the program specified by command within Button().