Skip to content

Commit

Permalink
refactor buttonbox: Hotkey events reunite with button events.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjdenis committed Oct 17, 2016
1 parent 20c2603 commit bff0d24
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 33 deletions.
25 changes: 1 addition & 24 deletions easygui/boxes/button_box_controller.py
Expand Up @@ -17,7 +17,7 @@ def x_pressed(self):
def escape_pressed(self, event):
self.nothing_selected_and_stop()

def button_pressed(self, choice):
def button_or_hotkey_pressed(self, choice):
# If cancel
if choice.is_cancel:
self.nothing_selected_and_stop()
Expand All @@ -26,29 +26,6 @@ def button_pressed(self, choice):
self.model.choices.selected_choice = choice
self.model.check_callback_updated()

def hotkey_pressed(self, keysym, keychar):
"""
Handle an event that is generated by a person interacting with a button. It may be a button press
or a key press.
TODO: Enhancement: Allow hotkey to be specified in filename of image as a shortcut too!!!
"""
if keysym != keychar: # A special character
hotkey_pressed = '<{}>'.format(keysym)
else:
hotkey_pressed = keysym

choice_found = self.model.choices.select_choice_from_hotkey(hotkey_pressed)

# If hotkey is in uppercase, it will work if the lowercase key is pressed
if not choice_found:
choice_found = self.model.choices.select_choice_from_hotkey(hotkey_pressed.upper())

if choice_found:
self.model.check_callback_updated()
else:
print("Event not understood")

def image_pressed(self, filename, row, column):
self.model.choices.unselect_choice()
self.model.row_column_selected = (row, column)
Expand Down
16 changes: 7 additions & 9 deletions easygui/boxes/button_box_view.py
Expand Up @@ -231,15 +231,12 @@ def create_buttons_frame(self):

def create_buttons(self, choices):

def create_command(button_text):
def command():
return self.controller.button_pressed(button_text)
def create_command(choice):
def command(event):
self.remember_window_position()
return self.controller.button_or_hotkey_pressed(choice)
return command

def command_when_hotkey_pressed(event):
self.remember_window_position()
return self.controller.hotkey_pressed(event.keysym, event.char)

# Create buttons dictionary and Tkinter widgets
buttons = dict()

Expand Down Expand Up @@ -268,13 +265,14 @@ def command_when_hotkey_pressed(event):

buttons[choice.unique_text] = button

command_when_hotkey = create_command(choice)
# Bind hotkey
if choice.hotkey:
self.boxRoot.bind_all(choice.hotkey, command_when_hotkey_pressed, add=True)
self.boxRoot.bind_all(choice.hotkey, command_when_hotkey, add=True)

# Also bind to its lowercase version if exists
if choice.lowercase_hotkey:
self.boxRoot.bind_all(choice.lowercase_hotkey, command_when_hotkey_pressed, add=True)
self.boxRoot.bind_all(choice.lowercase_hotkey, command_when_hotkey, add=True)

return buttons

0 comments on commit bff0d24

Please sign in to comment.