in given answer Here i saw use of "all" in tkinter's bindtags but can't figure out for what it stands for. is it the topmost level of whole application or something else?
CodePudding user response:
It stands for "all widgets". It's just a string that all widgets by default have as part of their binding tags. It is useful when you want to create a binding that will be in effect for all widgets in the application as a whole.
When you call bind
, you aren't actually binding to the widget per se. You're binding to a tag that corresponds to the widget. For example, if you create an Entry
widget, the binding tags for that widget might look like ('.!entry', 'Entry', '.', 'all')
.
Note: if you want to see the binding tags for any widget, print the results of the bindtags
method (eg: print(my_widget.bindtags())
)
The first element in the list is the widget itself, represented as the internal name of the widget. It is followed by the widget class, the window that the widget is in (in this case, '.' which stands for the root window), and then the special tag "all". It is possible for you to add your own tags, but there's rarely a need to do so.
When an event occurs in a widget, tkinter will iterate over the binding tags looking for a binding. For example, if you click on a button it will see if you have a binding on the widget itself. It will then check to see if there is a binding for the Button class. It will next check to see if there are any bindings on the window as a whole. Finally, it will check to see if there are bindings on the "all" tag.
In this way it's possible to add a binding that works for the whole application, for every widget within a window, for ever widget of a particular class, or for a specific widget. You can also add your own tags, or remove the default tags. For example, to remove all built-in bindings for an Entry widget, you can remove the window class from the widgets binding tags. It will then have no default bindings.