Get all default bindings for Button widget?

Get all default bindings for Button widget?

Problem Description:

Is there a way to get all default bindings for the tk.Button widget in tkinter?
Something like this (pseudo code):

>>> all_bindings = root.get_default_bindings(tk.Button) 
>>> print(all_bindings)
{"tk.Button": {"<<SelectAll>>" : "<Control-/>", "<<SelectNone>>" : "<Control-backslash>", "<<Pressed>>" : "<Button-1"}}

I think they call it in TCL "common virtual events"

Solution – 1

Is there a way to get all default bindings for the tk.Button widget in tkinter?

If you call bind_class giving it the Button class, it will return a tuple of bindings.

print(root.bind_class("Button"))

When I run the above I get the following:

('<ButtonRelease-1>', '<Button-1>', '<Leave>', '<Enter>', '<<Invoke>>', '<Key-space>')

If you want to see what code is bound to an event, you can pass the event along with the class:

print(root.bind_class("Button", "<<Invoke>>"))

If you do that on default bindings you’re likely to get a string representing one or more tcl commands. If you do it on a custom binding, you’ll get the name of an auto-generated function that calls a python function.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject