Is it possible to bind all widgets to one command, with a single line? It would be nice if I could type in one line as opposed to doing each widget individually.
4 Answers
You would use the bind_all method on the root window. This will then apply to all widgets (unless you remove the bindtag "all" from some widgets). Note that these bindings fire last, so you can still override the application-wide binding on specific widgets if you wish.
Here's a contrived example:
import Tkinter as tk
class App:
def __init__(self):
root = tk.Tk()
root.bind_all("<1>", self.woot)
label1 = tk.Label(text="Label 1", name="label1")
label2 = tk.Label(text="Label 2", name="label2")
entry1 = tk.Entry(name="entry1")
entry2 = tk.Entry(name="entry2")
label1.pack()
label2.pack()
entry1.pack()
entry2.pack()
root.mainloop()
def woot(self, event):
print "woot!", event.widget
app=App()
You might also be interested in my answer to the question How to bind self events in Tkinter Text widget after it will binded by Text widget? where I talk a little more about bindtags.

- 1
- 1

- 370,779
- 53
- 539
- 685
-
When I change `<1>` to `
`, does this work if Tinter is running in the background? – Tetsudou Mar 08 '15 at 02:57 -
@Tetsudou: it will only work when the application has the keyboard focus. – Bryan Oakley Mar 08 '15 at 04:45
-
@Bryan Oakley This does not work for the menu widget. – Сергей Кох Oct 31 '22 at 18:26
-
@СергейКох: what do you mean? Why do you think it doesn't work? Do you mean the binding doesn't work while the menu is present? – Bryan Oakley Oct 31 '22 at 18:27
-
@Bryan Oakley Yes, while the menu is active, the binding does not work in windows 10. – Сергей Кох Oct 31 '22 at 18:30
-
@СергейКох: that's not surprising. The menus on Windows are handled natively by windows rather than tkinter. – Bryan Oakley Oct 31 '22 at 18:51
If you have a list that contains all your widgets, you could iterate over them and assign the events.

- 12,561
- 6
- 44
- 51
You mean something like this code which handles all mouse events handled with single function?
from Tkinter import *
class ButtonHandler:
def __init__(self):
self.root = Tk()
self.root.geometry('600x500+200+200')
self.mousedown = False
self.label = Label(self.root, text=str(self.mousedown))
self.can = Canvas(self.root, width='500', height='400', bg='white')
self.can.bind("<Motion>",lambda x:self.handler(x,'motion'))
self.can.bind("<Button-1>",lambda x:self.handler(x,'press'))
self.can.bind("<ButtonRelease-1>",lambda x:self.handler(x,'release'))
self.label.pack()
self.can.pack()
self.root.mainloop()
def handler(self,event,button_event):
print('Handler %s' % button_event)
if button_event == 'press':
self.mousedown = True
elif button_event == 'release':
self.mousedown = False
elif button_event == 'motion':
if self.mousedown:
r = 5
self.can.create_oval(event.x-r, event.y-r, event.x+r, event.y+r, fill="orange")
self.label.config(text=str(self.mousedown))
button_event = ButtonHandler()

- 5,447
- 23
- 31
You could also just define a function that calls on all your widgets, and call that function. Or better yet create a class that call on your widgets in init and import the class...

- 347
- 2
- 6
- 16