Home > Net >  Get all default bindings for Button widget?
Get all default bindings for Button widget?

Time:12-07

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"

CodePudding user response:

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.

  • Related