Home > Software engineering >  Is it possible to bind multiple event handlers for the same event to a widget? Tkinter
Is it possible to bind multiple event handlers for the same event to a widget? Tkinter

Time:04-10

I want to bind the same key to the same widget but with different events but whenever I do this, the first event just gets ignored.

from tkinter import *

root = Tk()

c = StringVar()
c2 = StringVar()

def myfunction():
    c.set('Hello World')

def myfunction2():
    c2.set('Hello World, again')

root.bind('<Enter>', lambda event: myfunction()
label = Label(root, textvariable=c, bg='#0f0f0f', fg='white',
                       font=('@Yu Gothic Light', 12))
label.place(x=4, y=160)


root.bind('<Enter>', lambda event: myfunction2())
label2 = Label(root, textvariable=c2, bg='#0f0f0f', fg='white',
                       font=('@Yu Gothic Light', 12))  
label2.place(x=4, y=190)
# label 2 doesn't show anything and just gets ignored

root.mainloop()

What I'm trying to do is to set a label to a certain text when the cursor touches the root window. Please help.

CodePudding user response:

The default behavior of bind is to replace existing bindings.

If you want to add them, you need to use the add argument.

root.bind('<Enter>', lambda event: myfunction2(), add=" ")
  • Related