-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
207 lines (166 loc) · 7.38 KB
/
gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import pandas as pd
import customtkinter as tk
from data_processing import (
Recipe,
merge_ingredient_dictionaries,
flatten_dictionary,
convert_recipes_to_prompt
)
def option_menu_callback(choice):
choice
def clear_options_event(option_menu_objects: dict[tk.CTkOptionMenu], number_of_portions_menu: tk.CTkOptionMenu, textbox: tk.CTkTextbox):
for option in option_menu_objects:
option_menu_objects[option].set('')
number_of_portions_menu.set('Choose number of portions')
textbox.delete(index1='0.0', index2='500.0')
def generate_results_event(data: pd.DataFrame,
option_menu_objects: dict[tk.CTkOptionMenu],
number_of_portions: int,
to_excel: bool,
textbox: tk.CTkTextbox):
selected_recipes = []
for option in option_menu_objects.keys():
selected_option = option_menu_objects[option].get()
if selected_option != '':
recipe = Recipe(selected_option, data)
# This needs to be revised after adding extra 'number of portions' button
# For now program creates list for 2 persons by default
if recipe.double_portion:
selected_recipes.append(recipe.get_ingredients())
else:
for _ in range(number_of_portions):
selected_recipes.append(recipe.get_ingredients())
merged_recipes = merge_ingredient_dictionaries(selected_recipes)
result_dataframe = (
pd.DataFrame()
.from_dict(
flatten_dictionary(merged_recipes),
orient='index',
columns=['Ilość']
)
.sort_index()
)
if result_dataframe.empty:
textbox.delete(index1='0.0', index2='500.0')
textbox.insert(index='0.0', text='No recipes selected.')
print('No recipes selected.')
else:
if to_excel:
result_dataframe.to_excel('Lista zakupów.xlsx')
else:
convert_recipes_to_prompt(merged_recipes, textbox)
print(result_dataframe)
def convert_recipes_to_prompt(merged_recipes: dict, textbox: tk.CTkTextbox):
textbox.delete(index1='0.0', index2='500.0')
unit_index = 25
value_index = 15
for i, (ingredient, units) in enumerate(merged_recipes.items()):
for item in units.items():
unit = f'[{item[0]}]'
value = f'{item[1]}'
# Transforms items into evenly spaced table-like string
string = f'''{ingredient}{' '*(unit_index-len(ingredient))}{unit}{' '*(value_index-len(unit))}{value}\n'''
textbox.insert(index=f'{i}.0', text=string)
def build_option_menu(
week_frame: tk.CTkFrame,
day_index: int,
meal_index: int,
item_list: list[str],
option_menu_objects: dict[tk.CTkOptionMenu],
color: str):
'''
Creates option menu object. It stores name of the dish selected for given day and meal.
'''
#item_list.append('')
option_menu = tk.CTkOptionMenu(
week_frame,
values= item_list + [''],
command=option_menu_callback,
variable=tk.StringVar(),
corner_radius=0,
anchor='w',
dynamic_resizing=False,
font=('Arial', 10),
button_color=color,
fg_color=color
)
option_menu.set('')
option_menu.grid(
row=meal_index,
column=day_index,
sticky='we'
)
option_menu_objects.update({f'{day_index}_{meal_index}' : option_menu})
def build_week_frame_table(root: tk.CTk, option_menu_objects: dict[tk.CTkOptionMenu], settings: dict) -> tk.CTkFrame:
'''
Creates a table containing option menu objects
'''
week_frame = tk.CTkFrame(root)
for i in range(8):
week_frame.columnconfigure(i, weight=1)
weekdays = ['','Pn', 'Wt', 'Śr', 'Czw', 'Pt', 'So', 'Nd']
meals = ['', 'Śniadanie', 'Brunch', 'Obiad', 'Zapychacz', 'Surówka', 'Podwieczorek', 'Kolacja']
for day_index, day in enumerate(weekdays):
for meal_index, meal in enumerate(meals):
if day_index == 0:
tile = tk.CTkLabel(week_frame, text=meal, font=("Arial", 10))
tile.grid(row=meal_index, column=day_index, sticky='we')
elif meal_index == 0:
tile = tk.CTkLabel(week_frame, text=day, font=("Arial", 10))
tile.grid(row=meal_index, column=day_index, sticky='we')
else:
if meal == 'Śniadanie':
build_option_menu(
week_frame,
day_index=day_index,
meal_index=meal_index,
item_list=settings['breakfast'].get('item_list'),
option_menu_objects=option_menu_objects,
color=settings['breakfast'].get('button_color')
)
elif meal == 'Brunch' or meal == 'Podwieczorek':
build_option_menu(
week_frame,
day_index=day_index,
meal_index=meal_index,
item_list=settings['intermeal'].get('item_list'),
option_menu_objects=option_menu_objects,
color=settings['intermeal'].get('button_color')
)
elif meal == 'Obiad':
build_option_menu(
week_frame,
day_index=day_index,
meal_index=meal_index,
item_list=settings['lunch_main'].get('item_list'),
option_menu_objects=option_menu_objects,
color=settings['lunch_main'].get('button_color')
)
elif meal == 'Zapychacz':
build_option_menu(
week_frame,
day_index=day_index,
meal_index=meal_index,
item_list=settings['lunch_filler'].get('item_list'),
option_menu_objects=option_menu_objects,
color=settings['lunch_filler'].get('button_color')
)
elif meal == 'Surówka':
build_option_menu(
week_frame,
day_index=day_index,
meal_index=meal_index,
item_list=settings['lunch_salad'].get('item_list'),
option_menu_objects=option_menu_objects,
color=settings['lunch_salad'].get('button_color')
)
elif meal == 'Kolacja':
build_option_menu(
week_frame,
day_index=day_index,
meal_index=meal_index,
item_list=settings['dinner'].get('item_list'),
option_menu_objects=option_menu_objects,
color=settings['dinner'].get('button_color')
)
return week_frame