Skip to content

Commit

Permalink
Update ui.py
Browse files Browse the repository at this point in the history
Added option to press enter/return to continue
  • Loading branch information
dversoza committed Jun 21, 2021
1 parent 16446da commit c4634f7
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@

class BaseUI:
# Base user interface
def __init__(self, root, width=None, height=None, title=None, *args, **kwargs) -> None:
def __init__(self, root, width=None, height=None, *args, **kwargs) -> None:
"""Pass options to base UI using kwargs"""
self.app_width = width if width else APP_DEFAULT_WIDTH
self.app_height = height if height else APP_DEFAULT_HEIGHT
self.title = title if title else APP_TITLE

# Sets config through Tkinter
root.title(self.title)

# Centers UI window to user screen
self.center_window(root)
Expand Down Expand Up @@ -53,12 +49,18 @@ def __init__(self, root) -> None:
self.main_interface()

def main_interface(self):
def btn_handler(event=None):
self.start_game()

_frame = self.mainframe(self.root)
self.root.title(APP_TITLE)

Label(_frame, text='Seja bem-vindo ao AkiFood!').grid()
Label(_frame, text='Pense em um prato que gosta ...').grid()
Button(_frame, text="OK", padx=25, pady=5,
command=self.start_game).grid()
Button(_frame, text="OK", padx=25, pady=5, command=btn_handler).grid()

self.root.bind('<Return>', btn_handler)
_frame.focus()

def start_game(self):
# Calls Game class and manage runtime
Expand Down Expand Up @@ -104,6 +106,7 @@ def true_btn():
_window.destroy()

def false_btn():
self.root.title('Errei... Desisto')
self.new_dish_form(wrong_dish=dish)
_window.destroy()

Expand All @@ -120,34 +123,34 @@ def false_btn():
self.root.wait_window(_window)

def new_dish_form(self, wrong_dish: dict = None):
def button_handler():
def btn_handler(event=None):
# Prompts new adjective form only if wrong dish attempted
if wrong_dish:
self.new_adjective_form(new_dish_name.get(), wrong_dish)
_window.destroy()

# If no dish attempted (no options availabe), only creates a new one
else:
self.game.create_new_dish(new_dish_name.get())
_window.destroy()
self.reload_game()

_window, _frame = self.popup(self.root)

self.center_window(_window)
Label(_frame, text='Aproveitando que você ainda está ai...').grid()
Label(_frame, text='Em que prato pensou?').grid()

new_dish_name = StringVar()
new_dish_name_entry = Entry(_frame, textvariable=new_dish_name)
new_dish_name_entry.grid()
_entry = Entry(_frame, textvariable=new_dish_name)
_entry.grid()
_entry.focus()

new_dish_name_entry.focus()
Button(_window, text="OK", padx=25, pady=5, command=btn_handler).grid()

Button(_window, text="OK", padx=25, pady=5,
command=button_handler).grid()
self.root.bind("<Return>", button_handler)
_window.bind('<Return>', btn_handler)

def new_adjective_form(self, new_dish_name, wrong_dish):
def ok_btn():
def btn_handler(event=None):
self.game.get_smarter(wrong_dish=wrong_dish,
new_dish=new_dish_name,
new_adjective=new_adjective.get())
Expand All @@ -157,15 +160,18 @@ def ok_btn():
_window, _frame = self.popup(self.root)

Label(_frame, text="Se puder me ajudar a ficar mais inteligente ...").grid()
Label(
_frame, text=f"{new_dish_name} é ______, mas {wrong_dish['_name']} não.").grid()
Label(_frame,
text=f"{new_dish_name} é ______, mas {wrong_dish['_name']} não."
).grid()

new_adjective = StringVar()
_entry = Entry(_frame, textvariable=new_adjective)
_entry.grid()
_entry.focus()
ok_btn = Button(_frame, text="OK", padx=25, pady=5, command=ok_btn)
ok_btn.grid()

Button(_frame, text="OK", padx=25, pady=5, command=btn_handler).grid()

_window.bind('<Return>', btn_handler)

def reload_game(self):
# Updates db from last game run, preserving entries to next run
Expand Down

0 comments on commit c4634f7

Please sign in to comment.