-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_item_page.py
133 lines (112 loc) · 6.21 KB
/
add_item_page.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
from tkinter import *
import json
def invalid_quantity():
invalid_quantity = Tk()
invalid_quantity.geometry("1000x250")
invalid_quantity.title("Invalid Quantity")
Label(invalid_quantity, text="This is an invalid Quantity, Please input a whole number to continue",
font=("Times New Roman", 20, "bold")).grid(row=0)
def item_existance():
item_already_exists = Tk()
item_already_exists.geometry("1000x250")
item_already_exists.title("Item already Exists")
Label(item_already_exists, text="This item is already on your menu, please rename the item to continue",
font=("Times New Roman", 20, "bold")).grid(row=0)
def load_add_item_page():
# Generates Window
root = Tk()
root.geometry("1920x1080")
root.title("Add an Item to the Menu")
root.configure(background="#a4a4a4")
# Creates Labels to describe what the text entry boxes DO!
item_name_box_label = Label(root, width=60, bg='#c3c3c3', text="Item Name")
item_quantity_box_label = Label(root, width=60, bg='#c3c3c3', text="Item Quantity (Must be a Whole Number")
item_category_box_label = Label(root, width=60, bg='#c3c3c3', text="Item Category (Categories cannot be deleted once added)")
item_description_box_label = Label(root, width=60, bg='#c3c3c3', text="Item Description")
item_price_box_label = Label(root, width=60, bg='#c3c3c3', text="Item Price (Must be a Number")
# Generates Text input boxes and buttons
item_name_box = Entry(root, width=100, bg='#a4a4a4', font=("Times New Roman", 20, "bold"))
item_quantity_box = Entry(root, width=100, bg='#a4a4a4', font=("Times New Roman", 20, "bold"))
item_category_box = Entry(root, width=100, bg='#a4a4a4', font=("Times New Roman", 20, "bold"))
item_price_box = Entry(root, bg='#a4a4a4', width=100, font=("Times New Roman", 20, "bold"))
item_description_box = Entry(root, width=100, bg='#a4a4a4', font=("Times New Roman", 20, "bold"))
submit_button = Button(root, text="Submit", width=50, bg="#40bd40",
command=lambda: save_items_to_json(item_name_box, item_description_box, item_quantity_box,
item_price_box, item_category_box))
back_button = Button(root, text=" Back ", command=lambda: root.destroy(), font=("Times New Roman", 15, 'bold'),
bg="#c21313")
# Puts all previously generated labels into the load item window
item_name_box_label.grid(row=0, column=0)
item_quantity_box_label.grid(row=1, column=0)
item_category_box_label.grid(row=2, column=0)
item_description_box_label.grid(row=3, column=0)
item_price_box_label.grid(row=4, column=0)
submit_button.grid(row=5, column=1, sticky=W, pady=2)
back_button.grid(row=5, column=0, sticky=W, pady=2)
# Puts all previously generated text boxes and buttons on the window
item_name_box.grid(row=0, column=1)
item_quantity_box.grid(row=1, column=1)
item_category_box.grid(row=2, column=1)
item_description_box.grid(row=3, column=1)
item_price_box.grid(row=4, column=1)
# Configures the Input boxes labels to fill the page
item_description_box_label.config(font=("Times New Roman", 20, "bold"))
item_quantity_box_label.config(font=("Times New Roman", 20, "bold"))
item_category_box_label.config(font=("Times New Roman", 20, "bold"))
item_price_box_label.config(font=("Times New Roman", 20, "bold"))
item_name_box_label.config(font=("Times New Roman", 20, "bold"))
item_price_box.config(font=("Times New Roman", 20, "bold"))
item_description_box.config(font=("Times New Roman", 20, "bold"))
item_category_box.config(font=("Times New Roman", 20, "bold"))
item_quantity_box.config(font=("Times New Roman", 20, "bold"))
item_name_box.config(font=("Times New Roman", 20, "bold"))
submit_button.config(font=("Times New Roman", 20, "bold"), height=5, width=60)
back_button.config(font=("Times New Roman", 20, "bold"), height=5, width=60)
def check_item(item_name_box):
item_list = []
with open("items.json", "r") as json_file:
json_file_thing = json.load(json_file)
item_list = item_name_box.get().lower()
item_list2 = "".join(item_list)
item_list3 = item_list2.split(", ")
for item in item_list3:
for i in json_file_thing:
for j in json_file_thing[i]:
if j["item name"] == item:
return True
def save_items_to_json(item_name_box, item_description_box, item_quantity_box, item_price_box, item_category_box):
# Checks if any items with the same name already exist
if check_item(item_name_box):
item_existance()
elif not str(item_quantity_box.get()).isnumeric():
invalid_quantity()
else:
# Puts all user inputs in dictionary
json_object = {
"item name": item_name_box.get().lower(),
"item quantity": item_quantity_box.get().lower(),
"item description": item_description_box.get().lower(),
"item price": item_price_box.get().lower()
}
# Loads Json file and checks its contents
with open("items.json", "r") as json_file:
json_file_thing = json.load(json_file)
# Checks if category is in json file and if not adds category
if not item_category_box.get().lower() in json_file_thing:
json_file_thing[item_category_box.get().lower()] = []
json_file_thing[item_category_box.get().lower()].append(json_object)
test = json.dumps(json_file_thing, indent=4)
# Writes users input to Json file
with open("items.json", "w") as json_file:
json_file.write(test)
popup()
def popup():
confirmation = Toplevel()
confirmation.geometry("500x250")
confirmation.title("Item Added")
confirmation_label = Label(confirmation, text="This item has been added to the menu",
font=("Times New Roman", 20, "bold"))
confirmation_button = Button(confirmation, text="Continue", font=("Times New Roman", 20, "bold"),
command=lambda: confirmation.destroy())
confirmation_label.grid(row=0, column=3)
confirmation_button.grid(row=2, column=3)