-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
324 lines (280 loc) · 12.2 KB
/
main.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
from tkinter import Tk, Frame, Label, Button, LEFT, RIGHT, HORIZONTAL
from tkinter.ttk import Progressbar
from logic import FridgeMon, DoorButton, Tag, FOOD_LABELS
from threading import Thread
from time import sleep
# Colors
RED = '#dc1f1f'
GREEN = '#84BD93'
BG = "#FEF9EF"
BUTTON_BACKGROUND = '#F5D3BB'
TEXT_COLOR = '#514e4e'
LIGHT_BROWN = '#73665C'
# Screen parameters
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 420
REFRESH_TIME = 30 # seconds, rate at which the gui updates
ITEM_BUTTON_WIDTH = 228
class UI:
""""
Produces the graphical user interface for FridgeMon
"""
def __init__(self, mon: FridgeMon):
self.mon = mon
self.master = Tk()
title = 'FridgeMon'
self.master.title(title)
self.master.configure(bg=BG)
self.master.geometry(f"{SCREEN_WIDTH}x{SCREEN_HEIGHT}")
self.run = True
# timer-related attributes
self.time_since_refresh = 0
self.start_timer()
# create the initial display
self.start_item_screen()
def start_item_screen(self):
self.active_screen = 'items'
# define some frames and labels
self.main_frame = Frame(self.master, padx=10, bg=BG)
self.title_frame = Frame(self.main_frame, padx=10, bg=BG)
self.title_label = Label(self.title_frame, text='FridgeMon', font=('Inter', 30, 'bold'), bg=BG, fg=TEXT_COLOR)
self.status_label = Label(self.title_frame, text='Door Closed', fg = "#84BD93", font=('Inter', 20), padx=10, bg=BG)
text = 'Reduce waste and take control of your fridge!'
self.tagline_label = Label(self.title_frame, text=text, font=('Inter', 12), bg=BG, fg=TEXT_COLOR)
self.item_frame = Frame(self.main_frame, padx=10, bg=BG)
# grid the frames and labels
self.main_frame.grid(row=0, column=0, sticky='nw')
self.title_frame.grid(row=0, column=0, sticky='nw')
self.title_label.grid(row=0, column=0, sticky='nw')
self.status_label.grid(row=0, column=1, sticky='nw')
self.tagline_label.grid(row=1, column=0, sticky='nw')
self.item_frame.grid(row=1, column=0, sticky='nw')
self.refresh_item_buttons()
def start_label_screen(self, tag):
self.active_screen = 'label'
self.progress_bar = None
# clear the screen
for i in self.main_frame.winfo_children():
i.destroy()
# create some frames and labels labels
self.main_frame = Frame(self.master, padx=10, bg=BG)
self.top_frame = Frame(self.main_frame, padx=10, bg=BG)
self.bottom_frame = Frame(self.main_frame, bg=BG)
self.left_bottom_frame = Frame(self.bottom_frame, bg=BG, padx=10, pady=10)
self.right_bottom_frame = Frame(self.bottom_frame, padx=10, bg=BG)
text = f'Label {tag.name}'
font = ('Inter', 20, 'bold')
self.label_page_heading = Label(self.top_frame, text=text, font=font, bg=BG, fg=TEXT_COLOR)
self.icon_label = Label(self.left_bottom_frame,
image=tag.get_icon(),
width=ITEM_BUTTON_WIDTH,
bg=BG,
)
self.keep_as_last_button = Button(self.left_bottom_frame,
foreground=BG,
activeforeground=BG,
background=GREEN,
activebackground=GREEN,
command=lambda tag=tag: self.click_keep_as_last_button(tag),
font=('Inter', 16),
)
self.back_button = Button(self.left_bottom_frame,
foreground=BG,
activeforeground=BG,
background=LIGHT_BROWN,
activebackground=LIGHT_BROWN,
text='Back',
height=3,
font=('Inter', 16),
width=14,
command=lambda self=self: self.return_to_item_screen(),
)
# grid the frames and labels
self.main_frame.grid(row=0, column=0, sticky='nsew')
self.top_frame.grid(row=0, column=0, sticky='nsew')
self.bottom_frame.grid(row=1, column=0, sticky='nsew')
self.left_bottom_frame.grid(row=0, column=0, sticky='nsew')
self.right_bottom_frame.grid(row=0, column=1, sticky='nsew')
self.label_page_heading.grid(row=0, column=0, sticky='nsew')
self.icon_label.grid(row=0, column=0, sticky='nsew')
self.back_button.place(x=0,y=264)
# if the tag has been previously inserted, add a "keep as last" button
# that allows the user to retain the previous contents and start date
if tag.previous_label is not None and tag.label is None:
days_in_fridge = tag.get_previous_days_in_fridge()
text = f'Keep as:\n{tag.previous_label}\n({days_in_fridge}'
if days_in_fridge == 0 or days_in_fridge > 1:
text += ' days)'
else:
text += ' day)'
self.keep_as_last_button.config(text=text)
self.keep_as_last_button.grid(row=2, column=0, sticky='nsew')
column, row = 0, 0
for label in FOOD_LABELS:
button = Button(self.right_bottom_frame,
text=label,
font=('Inter', 14),
background=BUTTON_BACKGROUND,
activebackground=BUTTON_BACKGROUND,
foreground=TEXT_COLOR,
activeforeground=TEXT_COLOR,
width=19,
height=2,
command = lambda tag=tag, label=label: self.click_label_button(tag, label)
)
button.grid(row=row, column=column, sticky='nsew')
if row == 5:
row = 0
column += 1
else:
row += 1
def refresh_item_buttons(self):
"""
Refreshes the item buttons on the home screen
"""
# check if the active screen is the items screen
# if not, there is nothing to refresh, so do nothing
if self.active_screen != 'items':
return
# clear the current contents of the frame
for button in self.item_frame.winfo_children():
button.destroy()
# sort the list of tags
tags_in_fridge = [tag for tag in self.mon.tags.values() if tag.is_in_fridge]
tags_in_fridge.sort(key=lambda tag: tag.start_date, reverse=False)
# show unlabel,recently added items first
show_first = [tag for tag in tags_in_fridge if tag.get_seconds_in_fridge() <= REFRESH_TIME and not tag.is_labeled()]
# next, show items that were not recently added to fridge and all labeled items
show_next = [tag for tag in tags_in_fridge if tag.get_seconds_in_fridge() > REFRESH_TIME or tag.is_labeled()]
tags_in_fridge = show_first + show_next
# add some default text if there are no tags
if not tags_in_fridge:
text='No items found in fridge.'
empty_label = Label(self.item_frame, text=text, font=('Inter', 15), padx=20, bg=BG, fg=TEXT_COLOR)
empty_label.grid(row=0, column=0)
# re-grid the buttons
column, row = 0, 0
for tag in tags_in_fridge:
days_in_fridge = tag.get_days_in_fridge()
# the tag label is none, show "???"
if tag.label is None:
label = '???'
else:
label = tag.label
# text for the button
if tag in show_first:
button_text = f'Just added\nLabel it!'
elif days_in_fridge == 1:
button_text = f'1 day\n{label}'
else:
button_text = f'{days_in_fridge} days\n{label}'
# color for the text
if days_in_fridge < 4:
foreground = TEXT_COLOR
else:
foreground = RED
# background color of the button
if tag in show_first:
color = GREEN
else:
color = BUTTON_BACKGROUND
# create the food item button
button = Button(self.item_frame,
image=tag.get_icon(),
text=button_text,
background=color,
activebackground=color,
foreground=foreground,
activeforeground=foreground,
font=('Inter', 15, 'bold'),
compound=RIGHT,
padx=10,
pady=10,
width=ITEM_BUTTON_WIDTH,
anchor ='w',
justify = LEFT,
command = lambda tag=tag: self.start_label_screen(tag)
)
button.grid(column=column, row=row)
# update column and row count
if column >= 2:
column = 0
row += 1
else:
column += 1
def click_label_button(self, tag: Tag, label: str):
"""
Select a label for the tag and return to the home screen
"""
tag.label_contents(label)
self.return_to_item_screen()
def click_keep_as_last_button(self, tag):
tag.keep_as_last()
self.return_to_item_screen()
def return_to_item_screen(self):
self.main_frame.destroy()
self.start_item_screen()
def show_open(self):
"""
Displays text on screen to indicate the door is open.
"""
text = "Door Open"
fg = RED
self.status_label.config(text=text, fg=fg)
def show_closed(self):
"""
Displays text on screen to indicate the door is closed.
"""
if self.active_screen != 'items':
return
text = "Door Closed"
fg = GREEN
self.status_label.config(text=text, fg=fg)
def start_progress_bar(self):
"""
Puts an indeterminate progress bar on the screen and starts the animation.
For indicating that RFID scanning is in progress.
"""
text = "Scanning"
fg = TEXT_COLOR
self.status_label.config(text=text, fg=fg)
self.progress_bar = Progressbar(self.title_frame, orient=HORIZONTAL, length=200, mode='indeterminate')
self.progress_bar.grid(row=0, column=2, padx=10)
self.progress_bar.start(2)
def stop_progress_bar(self):
"""
Stops the progress bar
Call this if the animation needs to be interrupted
"""
if self.active_screen != 'items' or self.progress_bar is None:
return
self.progress_bar.grid_forget()
def start_timer(self):
"""
Spawns a new thread that keeps track of time and periodically
refreshes the screen
"""
freq = .1 # seconds
def thread():
while self.run:
sleep(freq)
self.time_since_refresh += freq
if self.time_since_refresh > REFRESH_TIME:
self.time_since_refresh = 0
self.refresh_item_buttons()
Thread(target=thread).start()
def mainloop(self):
"""
Runs the mainloop of the GUI program
"""
self.master.mainloop()
def stop(self) -> None:
self.run = False
mon = FridgeMon()
ui = UI(mon)
door_button = DoorButton(ui, mon)
door_button.start()
ui.mainloop()
# clean up
ui.stop()
door_button.stop()