Skip to content

Commit

Permalink
Fix default enter button key binding (israel-dryer#311)
Browse files Browse the repository at this point in the history
* add try-block around nametowidget call

* update default binding for enter key; added logic to handle Tcl/Tk widgets embedded in Messagebox, etc.
  • Loading branch information
israel-dryer authored Jul 14, 2022
1 parent 59f69f9 commit 5f755b0
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/ttkbootstrap/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ def apply_class_bindings(window: tkinter.Widget):

window.unbind_class("TButton", "<Key-space>")

def on_button_enter(event):
widget = window.nametowidget(event.widget)
widget.invoke()

window.bind_class("TButton", "<Key-Return>", on_button_enter)
window.bind_class("TButton", "<KP_Enter>", on_button_enter)
def button_default_binding(event):
"""The default keybind on a button when the return or enter key
is pressed and the button has focus or is the default button."""
try:
widget = window.nametowidget(event.widget)
widget.invoke()
except KeyError:
window.tk.call(event.widget, 'invoke')

window.bind_class("TButton", "<Key-Return>", button_default_binding,
add="+")
window.bind_class("TButton", "<KP_Enter>", button_default_binding, add="+")


def apply_all_bindings(window: tkinter.Widget):
Expand Down

0 comments on commit 5f755b0

Please sign in to comment.