Tkinter: binding the same key to different widgets Tkinter: binding the same key to different widgets tkinter tkinter

Tkinter: binding the same key to different widgets


Sure there is. Just bind it to two different widgets.

import Tkinterroot = Tkinter.Tk()def keypress1(event):    print event.keysym, " key pressed in root"def keypress2(event):    print event.keysym, " key pressed in text"text = Tkinter.Text(root, width=20, height=20)root.bind("<Return>", keypress1)text.bind("<Return>", keypress2)text.pack()root.mainloop()

Of course, the event has to actually occur in both widgets. I'm not sure whether there's a way to propagate an event captured by one widget to another that didn't capture it. But there are probably better ways to solve your problem than propagating events that way; you can always catch the event in root and do whatever you want there.