-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.py
541 lines (455 loc) · 25.1 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
from tkinter import Tk, Label, Button, Toplevel, StringVar, Entry, messagebox, Frame, END, Scrollbar, Y, RIGHT
from tkinter import Checkbutton, IntVar, BooleanVar, Text, INSERT
from functools import partial
import tkinter as tk
from tkinter.ttk import Separator, Style, Combobox, Treeview
from time import sleep
from snapFood import *
mydb = SnapFoodDB()
style = Style()
style.configure("mystyle.Treeview", highlightthickness=0, bd=0, font=('Calibri', 11)) # Modify the font of the body
style.configure("mystyle.Treeview.Heading", font=('Calibri', 13,'bold')) # Modify the font of the headings
style.layout("mystyle.Treeview", [('mystyle.Treeview.treearea', {'sticky': 'nswe'})]) # Remove the borders
class Application:
def __init__(self, master):
self.username = ""
self.password = ""
# self.db_cursor = mydb.cursor()
self.master = master
master.title("Snapp Food")
#Bring the window to the center of screen
windowWidth = master.winfo_reqwidth()
windowHeight = master.winfo_reqheight()
# print("Width",windowWidth,"Height",windowHeight)
positionRight = int(master.winfo_screenwidth()/2 - windowWidth/2)
positionDown = int(master.winfo_screenheight()/2 - windowHeight/2)
# Positions the window in the center of the page.
master.geometry("+{}+{}".format(positionRight, positionDown))
#Set Number of rows and columns
self.master.rowconfigure(5)
self.master.columnconfigure(5)
self.makeMainWindow()
# def loadingPage(self):
# # loader = Tk()
# # loader.title("loading")
# # root.attributes('-alpha', 1.0)
def makeMainWindow(self):
self.master.geometry("300x250")
self.master.title("Account Login")
Label(self.master,text="Select Your Choice", bg="blue", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
Button(self.master,text="Login", height="2", width="30", command = self.loginPage).pack()
Label(text="").pack()
Button(self.master, text="Register", height="2", width="30", command=self.registerPage).pack()
closeButton = Button(self.master, text="Exit", command=self.master.quit, width="200", height="2")
closeButton.pack()
def loginPage(self):
self.login_screen = Tk()
self.login_screen.title("Login")
self.login_screen.geometry("300x250")
Label(self.login_screen, text="Please enter details below to login").pack()
Label(self.login_screen, text="").pack()
username_verify = StringVar()
password_verify = StringVar()
Label(self.login_screen, text="Phone Numbers * ").pack()
username_login_entry = Entry(self.login_screen, textvariable=username_verify)
username_login_entry.pack()
Label(self.login_screen, text="").pack()
Label(self.login_screen, text="Password * ").pack()
password_login_entry = Entry(self.login_screen, textvariable=password_verify, show= '*')
password_login_entry.pack()
Label(self.login_screen, text="").pack()
Button(self.login_screen, text="Login", width=10, height=1, command = partial(self.loginVerify, username_login_entry, password_login_entry)).pack()
def loginVerify(self, username_login_entry, password_login_entry):
# self.db_cursor.execute("SELECT userid, password FROM USER WHERE userid=\'{}\'".format(username_login_entry.get()))
data = mydb.login(username_login_entry.get())
self.user_id = data[0][2]
self.phone_number = username_login_entry.get()
#check whether user_id exists or not
if (len(data) != 0 and data[0][1] == password_login_entry.get()):
self.dashboardPage()
else:
messagebox.showinfo('Username/Password incorrect', 'your username of password is incorrect')
def registerPage(self):
self.register_screen = Toplevel(self.master)
self.register_screen.title("Register")
self.register_screen.geometry("300x450")
Label(self.register_screen, text="Please enter details below", bg="blue").pack()
Label(self.register_screen, text="").pack()
#phonenumber
Label(self.register_screen, text="Phone Number * ").pack()
phone_number_entry = Entry(self.register_screen)
phone_number_entry.pack()
#password
Label(self.register_screen, text="Password * ").pack()
password_entry = Entry(self.register_screen,show='*')
password_entry.pack()
#repeat password
Label(self.register_screen, text="Password * ").pack()
repeat_password_entry = Entry(self.register_screen,show='*')
repeat_password_entry.pack()
#firstname
Label(self.register_screen, text="First Name * ").pack()
firstname_entry = Entry(self.register_screen)
firstname_entry.pack()
#lastname
Label(self.register_screen, text="Last Name * ").pack()
lastname_entry = Entry(self.register_screen)
lastname_entry.pack()
#email address
Label(self.register_screen, text="Email Address * ").pack()
email_entry = Entry(self.register_screen)
email_entry.pack()
Label(self.register_screen, text="").pack()
Button(self.register_screen, text="Register", width=10, height=1, bg="blue", command = partial(self.registerUser,
phone_number_entry ,password_entry, repeat_password_entry, firstname_entry, lastname_entry, email_entry)).pack()
def registerUser(self ,phone_number_entry, password_entry, repeat_password_entry, firstname_entry, lastname_entry, email_entry):
#insert into database
if (password_entry.get() == repeat_password_entry.get()):
mydb.registerUser( phone_number_entry.get(), password_entry.get(),firstname_entry.get(), lastname_entry.get(), email_entry.get())
Label(self.register_screen, text="Register was Seccesful").pack()
else:
messagebox.showinfo('Repeat password again', 'your repeated password doesn\'n match your entered password, Try Again')
# print (username_entry.get())
# print (password_entry.get())
# print (firstname_entry.get())
def dashboardPage(self):
self.master.destroy
self.login_screen.destroy
self.dashboard = Tk()
self.dashboard.title("User Dashboard")
self.dashboard.geometry("300x250")
Label(self.dashboard, text="Select Your Choice", bg="blue", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
Button(self.dashboard,text="Profile", height="2", width="30", command = self.profilePage).pack()
Label(text="").pack()
Button(self.dashboard,text="Restaurants", height="2", width="30", command=self.restaurantsPage).pack()
Label(text="").pack()
Button(self.dashboard,text="Order", height="2", width="30", command=self.orderPage).pack()
closeButton = Button(self.dashboard, text="Exit", command=self.dashboard.destroy, width="200", height="2")
closeButton.pack()
def profilePage(self):
def showAddress():
self.current_address_screen = Tk()
addresses = mydb.showUserAddress(self.user_id)
# print (addresses)
print (addresses)
for i in range(len(addresses)):
# print (addresses[i])
Label(self.current_address_screen, text="Address #"+str(i+1)+":").pack()
address_str = ""
address_str+=addresses[i][0] + ", "
for j in range(2,6):
address_str += addresses[i][j] + ", "
Label(self.current_address_screen, text=address_str).pack()
Button(self.current_address_screen, text="Delete this address",command=partial(self.deleteAddress, addresses[i][1])).pack()
self.profile = Tk()
self.profile.title("User Profile")
self.profile.geometry("700x1000")
"""
in this section we will show the user his/her information and he or she can change it
##########################################################3
"""
Label(self.profile, text="Your information", bg="red", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
user_information = mydb.showUser(self.user_id)
print (user_information)
# Show your firstname and edit
Label(self.profile, text="You FirstName:").pack()
firstname_entry = Entry(self.profile)
firstname_entry.pack()
firstname_entry.insert(END, user_information[0][1])
# Show your lastname and edit
Label(self.profile, text="You Lastname:").pack()
lastname_entry = Entry(self.profile)
lastname_entry.pack()
lastname_entry.insert(END, user_information[0][2])
# Show your phone number and edit
Label(self.profile, text="You Phone Number:").pack()
phone_number_entry = Entry(self.profile)
phone_number_entry.pack()
phone_number_entry.insert(END, user_information[0][3])
# Show your email address and edit
Label(self.profile, text="You email address:").pack()
email_address_entry = Entry(self.profile)
email_address_entry.pack()
email_address_entry.insert(END, user_information[0][4])
# Show your password and edit
Label(self.profile, text="You Password:").pack()
password_entry = Entry(self.profile)
password_entry.pack()
password_entry.insert(END, user_information[0][5])
# Change button
self.user_id = user_information[0][0]
Button(self.profile,text="Change my information", height="2", width="30", command=partial(self.updateUserInformation, user_information[0][0],
firstname_entry, lastname_entry, phone_number_entry, email_address_entry, password_entry)).pack()
# print (email_address_entry.get())
sep1= Separator(self.profile, orient=tk.HORIZONTAL)
sep1.pack(anchor="nw", fill=tk.X, pady=4)
"""
##########################################################
"""
"""
in this section we will show the user address
##########################################################3
"""
Label(self.profile, text="Address Management", bg="red", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
show_address_button = Button(self.profile, text="Show Current added address", command=showAddress)
show_address_button.pack()
add_address_button = Button(self.profile, text="Add Address", command=self.addAddress)
add_address_button.pack()
edit_address_button = Button(self.profile, text="Edit Exists Address", command=self.editAddresses)
edit_address_button.pack()
"""
##########################################################
"""
def deleteAddress(self, address_id):
# print (address_id)
mydb.deletAddress(address_id)
def editAddresses(self):
addresses = mydb.showUserAddress(self.user_id)
self.edit_address_screen = Tk()
self.edit_address_screen.title("Edit Address")
for i in range(len(addresses)):
Label(self.edit_address_screen, text="Address #"+str(i), bg="red", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
def addAddress(self):
def addNewAddress():
mydb.addAddress(x_entry.get(), y_entry.get(), self.user_id, mydb.addCity(city_combo.get()), street_entry.get()
,alley_entry.get(), plaque_entry.get(), other_information_entry.get())
self.add_address_screen = Tk()
self.add_address_screen.title("Adding new address")
self.add_address_screen.geometry("700x500")
Label(self.add_address_screen, text="First select your city and then").pack()
city_combo = Combobox(self.add_address_screen)
cities = mydb.showAllCity()
cities_list = []
for i in cities:
cities_list.append(i[1])
city_combo['values']= cities_list
city_combo.pack()
Label(self.add_address_screen, text="X:").pack()
x_entry = Entry(self.add_address_screen)
x_entry.pack()
Label(self.add_address_screen, text="Y:").pack()
y_entry = Entry(self.add_address_screen)
y_entry.pack()
Label(self.add_address_screen, text="Street:").pack()
street_entry = Entry(self.add_address_screen)
street_entry.pack()
Label(self.add_address_screen, text="Alley:").pack()
alley_entry = Entry(self.add_address_screen)
alley_entry.pack()
Label(self.add_address_screen, text="Plaque:").pack()
plaque_entry = Entry(self.add_address_screen)
plaque_entry.pack()
Label(self.add_address_screen, text="Other information:").pack()
other_information_entry = Entry(self.add_address_screen)
other_information_entry.pack()
add_address_button = Button(self.add_address_screen, text="Add This New Address", command=addNewAddress)
add_address_button.pack()
def updateUserInformation(self, user_id,firstname_entry, lastname_entry, phone_number_entry, email_address_entry, password_entry):
try:
mydb.updateUserProfile(user_id, firstname_entry.get(),lastname_entry.get(), phone_number_entry.get(), email_address_entry.get(), password_entry.get())
except:
print ("can't change the information")
Label(self.profile, text="Changed seccussful").pack()
def restaurantsPage(self):
def showRestaurants():
def OnDoubleClickOnFood(tree,foods, event):
item = tree.identify('item',event.x,event.y)
food_name = tree.item(item,"text")
food_id = -1
for food in foods:
if (food[3] == food_name):
food_id = food[0]
break
#Add the food to cart
self.cart_id = mydb.addFoodToCart(food_id, self.user_id)
def OnDoubleClick(tree, event):
item = tree.identify('item',event.x,event.y)
resturant_name = tree.item(item,"text")
resturant_id = -1
for res in resturants:
if (res[1] == resturant_name):
resturant_id = res[0]
# print (resturant_id)
foods = mydb.showFoodsOfShop(resturant_id)
if (len(foods) != 0):
self.menu_screen = Tk()
self.menu_screen.title(resturant_name+" Menu")
Label(self.menu_screen, text="Resturant Menu", bg="red", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
tree=Treeview(self.menu_screen,style="mystyle.Treeview")
tree["columns"]=("one","two","three", "four", "five")
#set tree columns
tree.column("#0", width=150, minwidth=150, stretch=tk.NO)
tree.column("one", width=400, minwidth=200)
tree.column("two", width=80, minwidth=50, stretch=tk.YES)
tree.column("three", width=80, minwidth=50, stretch=tk.YES)
tree.column("four", width=80, minwidth=50, stretch=tk.YES)
tree.column("five", width=80, minwidth=50, stretch=tk.YES)
#set tree's heading
tree.heading("#0", text="Name",anchor=tk.W)
tree.heading("one", text="Price",anchor=tk.W)
tree.heading("two", text="About",anchor=tk.W)
tree.heading("three", text="Category",anchor=tk.W)
tree.heading("four", text="Image",anchor=tk.W)
tree.heading("five", text="Discount",anchor=tk.W)
tree.pack()
for i in range(len(foods)):
tree.insert("", i+1, text=foods[i][3], values=(foods[i][1], foods[i][2], mydb.showCategoryName(foods[i][6])[0][0], foods[i][5], foods[i][4]))
tree.bind("<Double-1>", partial(OnDoubleClickOnFood,tree, foods))
address_id = -1
for variable in all_variables:
if (variable.get()):
index = all_variables.index(variable)
address_id = all_address[index][1]
self.address_id = address_id
resturants = mydb.searchShopByLocation(int(address_id), 100)
# print (resturants)
self.searched_resturant_screen = Tk()
self.searched_resturant_screen.title("Results")
tree=Treeview(self.searched_resturant_screen,style="mystyle.Treeview")
tree["columns"]=("one","two","three")
#set tree columns
tree.column("#0", width=270, minwidth=270, stretch=tk.NO)
tree.column("one", width=150, minwidth=150, stretch=tk.NO)
tree.column("two", width=400, minwidth=200)
tree.column("three", width=80, minwidth=50, stretch=tk.YES)
#set tree's heading
tree.heading("#0",text="Name",anchor=tk.W)
tree.heading("one", text="About",anchor=tk.W)
tree.heading("two", text="min bill value",anchor=tk.W)
tree.heading("three", text="address",anchor=tk.W)
list_of_resturant_in_treeview = []
for i in range(len(resturants)):
resturant_in_treeview = tree.insert("", i+1, text=resturants[i][1], values=(resturants[i][2], resturants[i][3], self.make_address_str(resturants[i][4])))
list_of_resturant_in_treeview.append(resturant_in_treeview)
# for resturant in list_of_resturant_in_treeview:
# index = list_of_resturant_in_treeview.index(resturant)
# shop_id = resturants[index][0]
# shop_foods = mydb.showFoodsOfShop(shop_id)
tree.pack(side=tk.TOP,fill=tk.X)
tree.bind("<Double-1>", partial(OnDoubleClick,tree))
#find element on double click
self.address_selection_screen = Tk()
self.address_selection_screen.title("Your addresses")
self.address_selection_screen.geometry("700x500")
Label(self.address_selection_screen, text="Choose an address to find the nearest resturant to you", bg="yellow", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
all_address = mydb.showUserAddress(self.user_id)
# print (all_address)
all_variables = []
list_of_address_str = []
for address in all_address:
address_str = ""
for part in address[1:-2]:
address_str += str(part) + ", "
list_of_address_str.append(address_str)
vari = IntVar(self.address_selection_screen)
all_variables.append(vari)
Checkbutton(self.address_selection_screen, text=address_str, variable=vari).pack()
Button(self.address_selection_screen, text="Submit", command=showRestaurants).pack()
def make_address_str(self, address_id):
address_info = mydb.showAddress(address_id)
address_info = address_info[0]
address_str = address_info[0] + ", "
# print (address_info)
for i in range(2,6):
address_str += address_info[i] + ", "
# print (address_str)
return address_str
def make_foods_str(self, foods_list):
food_str_dict = {}
for food in foods_list:
food_str = mydb.showFoods([food])[0][3]
if food_str in food_str_dict.keys():
food_str_dict[food_str] += 1
else:
food_str_dict[food_str] = 1
return_str = ""
for food_name in list(food_str_dict.keys()):
return_str += food_name + ": "
return_str += str(food_str_dict[food_name]) + " "
return return_str
def saveComment(self, order_id, comment_entry, rate_entry):
mydb.addComment(order_id, rate_entry.get(), comment_entry.get())
def orderPage(self):
def OnDoubleClickOnOrder(tree, orders_list, event):
item = tree.identify('item',event.x,event.y)
order_number = tree.item(item,"text")
order_id = orders_list [int(order_number[-1]) - 1]
self.order_comment_page = Tk()
self.order_comment_page.title("Comment")
Label(self.order_comment_page, text="Comment on this order", bg="yellow", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
Label(self.order_comment_page, text="Your comment:").pack()
comment_entry = Entry(self.order_comment_page)
comment_entry.pack()
Label(self.order_comment_page, text="Your rating to this order(from 0 to 100):").pack()
rate_entry = Entry(self.order_comment_page)
rate_entry.pack()
Button(self.order_comment_page, text="comment this", command=partial(self.saveComment, order_id, comment_entry, rate_entry)).pack()
def showHistoryOfOrders():
self.history_of_orders_screen = Tk()
self.history_of_orders_screen.title("Hisotry of orders")
Label(self.history_of_orders_screen, text="History of orders, click on them to comment", bg="yellow", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
tree=Treeview(self.history_of_orders_screen, style="mystyle.Treeview")
tree["columns"]=("one","two","three")
#set tree columns
tree.column("#0", width=270, minwidth=270, stretch=tk.NO)
tree.column("one", width=150, minwidth=150, stretch=tk.NO)
tree.column("two", width=400, minwidth=200)
tree.column("three", width=80, minwidth=50, stretch=tk.YES)
#set tree's heading
tree.heading("#0",text="Order#",anchor=tk.W)
tree.heading("one", text="Total Price",anchor=tk.W)
tree.heading("two", text="Address",anchor=tk.W)
tree.heading("three", text="Foods",anchor=tk.W)
history_of_orders = mydb.showBuyHistory(self.user_id)
invoic_dict = {}
for history in history_of_orders:
if ((history[0],history[1],history[2]) in invoic_dict.keys()):
invoic_dict[(history[0],history[1],history[2])].append(history[3])
else:
invoic_dict[(history[0],history[1],history[2])] = []
invoic_dict[(history[0],history[1],history[2])].append(history[3])
# print (type(list(invoic_dict.keys())))
# print (invoic_dict)
tree.pack(side=tk.TOP,fill=tk.X)
orders_list = []
for key in invoic_dict.keys():
orders_list.append(key[0])
for i in range(len(list(invoic_dict.keys()))):
address_id = list(invoic_dict.keys())[i][2]
foods_list = invoic_dict[list(invoic_dict.keys())[i]]
print (foods_list)
tree.insert("", i+1, text="Order#"+str(i+1), values=(list(invoic_dict.keys())[i][1], self.make_address_str(address_id), self.make_foods_str(foods_list)))
# resturant_in_treeview = tree.insert("", i+1, text=resturants[i][1], values=(resturants[i][2], resturants[i][3], make_address_str(resturants[i][4])))
# list_of_resturant_in_treeview.append(resturant_in_treeview)
tree.bind("<Double-1>", partial(OnDoubleClickOnOrder,tree, orders_list))
def finilizeCart(discount_code):
# print (self.address_id)
self.ivoice_id = mydb.finalizeCart(self.user_id, self.address_id, discount_code.get())
def showAllOrders():
print ("show all orders")
self.order_screen = Tk()
self.order_screen.title("Orders Page")
Label(self.order_screen, text="Finilize your Cart", bg="red", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
Label(self.order_screen, text="enter your discount code if you have one").pack()
discount_code = Entry(self.order_screen)
discount_code.pack()
Button(self.order_screen,text="Buy your Cart", height="2", width="30", command=partial(finilizeCart, discount_code)).pack()
Label(self.order_screen, text="Your Orders History (Compeleted)", bg="red", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
Button(self.order_screen,text="history of orders", height="2", width="30", command=showHistoryOfOrders).pack()
Label(self.order_screen, text="Your All Orders", bg="red", width="300", height="2", font=("Calibri", 13)).pack()
Label(text="").pack()
Button(self.order_screen,text="history of orders", height="2", width="30", command=showAllOrders).pack()
root = Tk()
my_gui = Application(root)
root.mainloop()