-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
346 lines (276 loc) · 12.4 KB
/
functions.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
'''
Inventory Manager
Nathan Tang
19/05/16
'''
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
import sqlalchemy
import os
class itemsClass:
def __init__(self):
self.item = None
self.ID = None
self.price = int
self.totAvail = totalAvailable()
self.totChecked = totalChecked()
self.description = str
def setItem(self, item):
self.item = item
return self.item
def setID(self, ID):
self.ID = ID
return self.ID
def setPrice(self, price):
self.price = price
return self.price
def setTotAvail(self, totAvail):
self.totAvail = totAvail
def setTotChecked(self, totChecked):
self.totChecked = totChecked
def setDescription(self, description):
self.description = description
def getItem(self):
return self.item
def getID(self):
return self.ID
def getPrice(self):
return self.price
def getTotAvail(self):
return self.totAvail
def getTotChecked(self):
return self.totChecked
def getDescription(self):
return self.description
class totalAvailable:
def __init__(self):
self.totAvail = int
def setTotAvail(self, totAvail):
self.totAvail = totAvail
def getTotAvail(self):
return self.totAvail
class totalChecked:
def __init__(self):
self.totChecked = int
def setTotChecked(self, totChecked):
self.totChecked = totChecked
def getTotChecked(self):
return self.totChecked
def openSQL(filename):
engine = sqlalchemy.create_engine('sqlite:///%s' % filename)
connection = engine.connect()
metadata = sqlalchemy.MetaData()
inventory = sqlalchemy.Table('inventory', metadata, autoload=True, autoload_with=engine)
print(inventory.columns.keys())
query = sqlalchemy.select([inventory])
resultProxy = connection.execute(query)
resultSet = resultProxy.fetchall()
print(resultSet)
return engine, connection, metadata, inventory, query, resultProxy, resultSet
def openFile(tree):
filename = (filedialog.askopenfilename(initialdir=os.getcwd(), filetypes=(("Database", "*.db"),("All Files", "*.*"))))
print(filename)
printTreeview(tree, filename)
return filename
def newFile(tree):
directory = filedialog.asksaveasfile(defaultextension=".db", filetypes=(("Database", "*.db"),))
print(directory.name)
engine = sqlalchemy.create_engine('sqlite:///%s' % directory.name)
connection = engine.connect()
metadata = sqlalchemy.MetaData()
inventory = sqlalchemy.Table('inventory', metadata,
sqlalchemy.Column('Item', sqlalchemy.String()),
sqlalchemy.Column('ID', sqlalchemy.String()),
sqlalchemy.Column('Price', sqlalchemy.Integer()),
sqlalchemy.Column('Available', sqlalchemy.Integer()),
sqlalchemy.Column('CheckedOut', sqlalchemy.Integer()),
sqlalchemy.Column('Description', sqlalchemy.String())
)
metadata.create_all(engine)
printTreeview(tree, directory.name)
return directory.name
def sort(tree, column, descending): # Allows user to sort the data
data = [(tree.set(child, column), child) for child in tree.get_children('')] # Places values to sort in data
try:
for i in range(len(data)):
data[i] = list(data[i])
data[i][0] = int(data[i][0])
except:
data[i] = tuple(data[i])
data.sort(reverse=descending) # Reorders data
for index, item in enumerate(data):
tree.move(item[1], '', index)
tree.heading(column, command=lambda col=column: sort(tree, column, int(not descending)))
def addMenu(tree, filename): # Add Menu
engine, connection, metadata, inventory, query, resultProxy, resultSet = openSQL(filename)
def getItemArgs(*args): # Get items in entry
item = stringvar1.get()
ID = stringvar2.get()
price = intVar1.get()
available = intVar2.get()
checkedOut = intVar3.get()
description = stringvar3.get()
if item and ID and price and available and checkedOut and description:
ContButton.config(state='normal', command=lambda: ContButtonFunc(item, ID, price, available, checkedOut, description))
else:
ContButton.config(state='disabled')
def ContButtonFunc(item, ID, price, available, checkedOut, description):
newItem = itemsClass()
newItem.setItem(item)
newItem.setID(ID)
newItem.setPrice(price)
newItem.setTotAvail(available)
newItem.setTotChecked(checkedOut)
newItem.setDescription(description)
# Write to tree
tree.insert("", len(tree.get_children()), values=(newItem.getItem(), newItem.getID(), newItem.getPrice(), newItem.getTotAvail(), newItem.getTotChecked(), newItem.getDescription()))
# Write to DB
query = sqlalchemy.insert(inventory)
values = [{'Item':newItem.getItem(), 'ID':newItem.getID(), 'Price':newItem.getPrice(), 'Available':newItem.getTotAvail(), 'CheckedOut':newItem.getTotChecked(), 'Description':newItem.getDescription()}]
resultProxy = connection.execute(query, values)
results = connection.execute(sqlalchemy.select([inventory])).fetchall()
root.destroy()
root = tk.Tk()
root.wm_title('Add A New Item')
root.focus_force()
root.configure(background='white')
labels=('Item: ', 'ID: ', 'Price: ', 'Available: ', 'Checked Out: ', 'Description: ')
for i in labels:
tk.Label(root, text=i, justify=tk.LEFT, bg='white', anchor='w').grid(row=labels.index(i))
stringvar1 = tk.StringVar(root)
stringvar2 = tk.StringVar(root)
stringvar3 = tk.StringVar(root)
intVar1 = tk.IntVar(root)
intVar2 = tk.IntVar(root)
intVar3 = tk.IntVar(root)
stringvar1.trace('w', getItemArgs)
stringvar2.trace('w', getItemArgs)
stringvar3.trace('w', getItemArgs)
intVar1.trace('w', getItemArgs)
intVar2.trace('w', getItemArgs)
intVar3.trace('w', getItemArgs)
item = tk.Entry(root, width=40, bg='white', textvariable=stringvar1)
ID = tk.Entry(root, width=40, bg='white', textvariable=stringvar2)
price = tk.Entry(root, width=40, bg='white', textvariable=intVar1)
available = tk.Entry(root, width=40, bg='white', textvariable=intVar2)
checkedOut = tk.Entry(root, width=40, bg='white', textvariable=intVar3)
description = tk.Entry(root, width=40, bg='white', textvariable=stringvar3)
item.grid(row=0, column=1)
ID.grid(row=1, column=1)
price.grid(row=2, column=1)
available.grid(row=3, column=1)
checkedOut.grid(row=4, column=1)
description.grid(row=5, column=1)
ContButton = tk.Button(root, text='Continue', bg='white', command=lambda: getItemArgs(item, ID, price, available, checkedOut, description))
ContButton.grid(row=6, column=1)
root.focus_force()
root.mainloop()
def editMenu(tree, filename): # Edit Menu
engine, connection, metadata, inventory, query, resultProxy, resultSet = openSQL(filename)
selected_item = tree.selection()[0]
treeItem = tree.item(selected_item)['values']
def getItemArgs(*args): # Get items in entry
item = stringvar1.get()
ID = stringvar2.get()
price = intVar1.get()
available = intVar2.get()
checkedOut = intVar3.get()
description = stringvar3.get()
if item and ID and price and available and checkedOut and description:
ContButton.config(state='normal', command=lambda: ContButtonFunc(item, ID, price, available, checkedOut, description))
else:
ContButton.config(state='disabled')
def ContButtonFunc(item, ID, price, available, checkedOut, description):
newItem = itemsClass()
newItem.setItem(item)
newItem.setID(ID)
newItem.setPrice(price)
newItem.setTotAvail(available)
newItem.setTotChecked(checkedOut)
newItem.setDescription(description)
# Write to tree
tree.insert("", len(tree.get_children()), values=(newItem.getItem(), newItem.getID(), newItem.getPrice(), newItem.getTotAvail(), newItem.getTotChecked(), newItem.getDescription()))
# Write to DB
query = sqlalchemy.insert(inventory)
values = [{'Item':newItem.getItem(), 'ID':newItem.getID(), 'Price':newItem.getPrice(), 'Available':newItem.getTotAvail(), 'CheckedOut':newItem.getTotChecked(), 'Description':newItem.getDescription()}]
resultProxy = connection.execute(query, values)
results = connection.execute(sqlalchemy.select([inventory])).fetchall()
root.destroy()
delItem(tree, filename)
root = tk.Tk()
root.wm_title('Edit')
root.focus_force()
root.configure(background='white')
labels=('Item: ', 'ID: ', 'Price: ', 'Available: ', 'Checked Out: ', 'Description: ')
for i in labels:
tk.Label(root, text=i, justify=tk.LEFT, bg='white', anchor='w').grid(row=labels.index(i))
stringvar1 = tk.StringVar(root)
stringvar2 = tk.StringVar(root)
stringvar3 = tk.StringVar(root)
intVar1 = tk.IntVar(root)
intVar2 = tk.IntVar(root)
intVar3 = tk.IntVar(root)
stringvar1.trace('w', getItemArgs)
stringvar2.trace('w', getItemArgs)
stringvar3.trace('w', getItemArgs)
intVar1.trace('w', getItemArgs)
intVar2.trace('w', getItemArgs)
intVar3.trace('w', getItemArgs)
item = tk.Entry(root, width=40, bg='white', textvariable=stringvar1)
ID = tk.Entry(root, width=40, bg='white', textvariable=stringvar2)
price = tk.Entry(root, width=40, bg='white', textvariable=intVar1)
available = tk.Entry(root, width=40, bg='white', textvariable=intVar2)
checkedOut = tk.Entry(root, width=40, bg='white', textvariable=intVar3)
description = tk.Entry(root, width=40, bg='white', textvariable=stringvar3)
item.insert(tk.END, treeItem[0])
ID.insert(tk.END, treeItem[1])
price.insert(tk.END, treeItem[2])
available.insert(tk.END, treeItem[3])
checkedOut.insert(tk.END, treeItem[4])
description.insert(tk.END, treeItem[5])
item.grid(row=0, column=1)
ID.grid(row=1, column=1)
price.grid(row=2, column=1)
available.grid(row=3, column=1)
checkedOut.grid(row=4, column=1)
description.grid(row=5, column=1)
ContButton = tk.Button(root, text='Continue', bg='white', command=lambda: getItemArgs(item, ID, price, available, checkedOut, description))
ContButton.grid(row=6, column=1)
root.focus_force()
root.mainloop()
def delItem(tree, filename): # Delete item
engine, connection, metadata, inventory, query, resultProxy, resultSet = openSQL(filename)
selected_item = tree.selection()[0] # Deletes selected item from tree and db file
treeItem = tree.item(selected_item)['values']
for i in range(len(resultSet)):
if resultSet[i] == tuple(treeItem):
connection.execute(inventory.delete().where(inventory.columns.Item == treeItem[0]))
break
tree.delete(selected_item)
def checkOut(tree, filename): # Check out an item
engine, connection, metadata, inventory, query, resultProxy, resultSet = openSQL(filename)
selected_item = tree.selection()[0]
treeItem = tree.item(selected_item)['values']
for i in range(len(resultSet)):
if resultSet[i] == tuple(treeItem):
connection.execute(sqlalchemy.update(inventory).values(
Available = treeItem[3] - 1, CheckedOut = treeItem[4] + 1).where(inventory.columns.Item == treeItem[0]))
break
printTreeview(tree, filename)
def checkIn(tree, filename): # Check in an item
engine, connection, metadata, inventory, query, resultProxy, resultSet = openSQL(filename)
selected_item = tree.selection()[0]
treeItem = tree.item(selected_item)['values']
for i in range(len(resultSet)):
if resultSet[i] == tuple(treeItem):
connection.execute(sqlalchemy.update(inventory).values(
Available = treeItem[3] + 1, CheckedOut = treeItem[4] - 1).where(inventory.columns.Item == treeItem[0]))
break
printTreeview(tree, filename)
def printTreeview(tree, filename): # Updates the treeview
engine, connection, metadata, inventory, query, resultProxy, resultSet = openSQL(filename)
tree.delete(*tree.get_children()) # Delete tree
# Writes updated tree
for i in range(len(resultSet)):
tree.insert("", i, values=(resultSet[i][0],resultSet[i][1],resultSet[i][2],resultSet[i][3],resultSet[i][4],resultSet[i][5]))