Skip to content

Commit

Permalink
Improved button functionality for Messagebox
Browse files Browse the repository at this point in the history
Fixes:
 - Value of the default button is now porperly respected (fix israel-dryer#525)
 - Fixed key bindings for Enter key (fix israel-dryer#526)

 New Features:
 - Add support for left/right arrow keys to select an answer
 - Automatic format selection based on the default (proposed as part of israel-dryer#525
  • Loading branch information
Slarag committed Jan 12, 2024
1 parent fdb5da5 commit 4c78fca
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions src/ttkbootstrap/dialogs/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ def __init__(

if buttons is None:
self._buttons = [
f"{MessageCatalog.translate('Cancel')}:secondary",
f"{MessageCatalog.translate('OK')}:primary",
f"{MessageCatalog.translate('Cancel')}",
f"{MessageCatalog.translate('OK')}",
]
else:
self._buttons = buttons
Expand Down Expand Up @@ -303,11 +303,20 @@ def create_buttonbox(self, master):

for i, button in enumerate(self._buttons[::-1]):
cnf = button.split(":")
text = cnf[0]

is_default = False
if self._default is not None and text == self._default:
is_default = True
elif self._default is None and i == 0:
is_default = True

if len(cnf) == 2:
text, bootstyle = cnf
bootstyle = cnf[1]
elif is_default:
bootstyle = 'primary'
else:
text = cnf[0]
bootstyle = "secondary"
bootstyle = 'secondary'

if self._localize == True:
text = MessageCatalog.translate(text)
Expand All @@ -318,14 +327,20 @@ def create_buttonbox(self, master):
btn.lower() # set focus traversal left-to-right
button_list.append(btn)

if self._default is not None and text == self._default:
self._initial_focus = btn
elif self._default is None and i == 0:
if is_default:
self._initial_focus = btn

# bind default button to return key press and set focus
self._toplevel.bind("<Return>", lambda _, b=btn: b.invoke())
self._toplevel.bind("<KP_Enter>", lambda _, b=btn: b.invoke())
# bind default button to return key press and set focus
btn.bind("<Return>", lambda _, b=btn: b.invoke())
btn.bind("<KP_Enter>", lambda _, b=btn: b.invoke())

for index, btn in enumerate(button_list):
if index > 0:
nbtn = button_list[index - 1]
btn.bind('<Right>', lambda _, b=nbtn:b.focus_set())
if index < len(button_list) - 1:
nbtn = button_list[index + 1]
btn.bind('<Left>', lambda _, b=nbtn:b.focus_set())

ttk.Separator(self._toplevel).pack(fill=X)
frame.pack(side=BOTTOM, fill=X, anchor=S)
Expand Down Expand Up @@ -845,7 +860,7 @@ def on_reset_date(self, *_):

def _set_window_position(self):
"""Move the window the to bottom-right of the parent widget, or
the top-left corner of the master window if no parent is
the top-left corner of the master window if no parent is
provided.
"""
if self.parent:
Expand Down Expand Up @@ -1288,7 +1303,7 @@ def show_question(
message,
title=" ",
parent=None,
buttons=["No:secondary", "Yes:primary"],
buttons=["No", "Yes"],
alert=True,
**kwargs,
):
Expand Down Expand Up @@ -1478,7 +1493,7 @@ def yesno(message, title=" ", alert=False, parent=None, **kwargs):
title=title,
message=message,
parent=parent,
buttons=["No", "Yes:primary"],
buttons=["No", "Yes"],
alert=alert,
localize=True,
**kwargs,
Expand Down Expand Up @@ -1528,7 +1543,7 @@ def yesnocancel(message, title=" ", alert=False, parent=None, **kwargs):
message=message,
parent=parent,
alert=alert,
buttons=["Cancel", "No", "Yes:primary"],
buttons=["Cancel", "No", "Yes"],
localize=True,
**kwargs,
)
Expand Down Expand Up @@ -1577,7 +1592,7 @@ def retrycancel(message, title=" ", alert=False, parent=None, **kwargs):
message=message,
parent=parent,
alert=alert,
buttons=["Cancel", "Retry:primary"],
buttons=["Cancel", "Retry"],
localize=True,
**kwargs,
)
Expand Down

0 comments on commit 4c78fca

Please sign in to comment.