· 5 years ago · Apr 28, 2020, 02:28 PM
1from tkinter import *
2import tkinter.messagebox as tkMessageBox
3import sqlite3
4import tkinter.ttk as ttk
5
6
7root = Tk()
8root.title("Vehicle Inventory System")
9
10width = 1024
11height = 520
12screen_width = root.winfo_screenwidth()
13screen_height = root.winfo_screenheight()
14x = (screen_width / 2) - (width / 2)
15y = (screen_height / 2) - (height / 2)
16root.geometry("%dx%d+%d+%d" % (width, height, x, y))
17root.resizable(0, 0)
18root.config(bg="#6666ff")
19
20# ========================================VARIABLES========================================
21USERNAME = StringVar()
22PASSWORD = StringVar()
23VEHICLE_NAME = StringVar()
24VEHICLE_MODEL = StringVar()
25VEHICLE_YEAR = IntVar()
26VEHICLE_PRICE = IntVar()
27VEHICLE_QTY = IntVar()
28VEHICLE_COLOR = StringVar()
29VEHICLE_MILEAGE = IntVar()
30SEARCH = StringVar()
31
32
33# ========================================METHODS==========================================
34
35def Database():
36 global conn, cursor
37 conn = sqlite3.connect("Vee.db")
38 cursor = conn.cursor()
39 cursor.execute(
40 "CREATE TABLE IF NOT EXISTS `admin` (admin_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT, password TEXT)")
41 cursor.execute(
42 "CREATE TABLE IF NOT EXISTS `vehicle` (vehicle_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, vehicle_name TEXT, vehicle_model TEXT, vehicle_year TEXT, vehicle_color TEXT, vehicle_mileage TEXT, vehicle_qty TEXT, vehicle_price TEXT)")
43 cursor.execute("SELECT * FROM `admin` WHERE `username` = 'admin' AND `password` = 'admin'")
44 if cursor.fetchone() is None:
45 cursor.execute("INSERT INTO `admin` (username, password) VALUES('admin', 'admin')")
46 conn.commit()
47
48
49def Exit():
50 result = tkMessageBox.askquestion('Vehicle Inventory System', 'Are you sure you want to exit?', icon="warning")
51 if result == 'yes':
52 root.destroy()
53 exit()
54
55
56def Exit2():
57 result = tkMessageBox.askquestion('Vehicle Inventory System', 'Are you sure you want to exit?', icon="warning")
58 if result == 'yes':
59 Home.destroy()
60 exit()
61
62
63def ShowLoginForm():
64 global loginform
65 loginform = Toplevel()
66 loginform.title("Vehicle Inventory System/Account Login")
67 width = 600
68 height = 500
69 screen_width = root.winfo_screenwidth()
70 screen_height = root.winfo_screenheight()
71 x = (screen_width / 2) - (width / 2)
72 y = (screen_height / 2) - (height / 2)
73 loginform.resizable(0, 0)
74 loginform.geometry("%dx%d+%d+%d" % (width, height, x, y))
75 LoginForm()
76
77
78def LoginForm():
79 global lbl_result
80 TopLoginForm = Frame(loginform, width=600, height=100, bd=1, relief=SOLID)
81 TopLoginForm.pack(side=TOP, pady=20)
82 lbl_text = Label(TopLoginForm, text="Administrator Login", font=('arial', 18), width=600)
83 lbl_text.pack(fill=X)
84 MidLoginForm = Frame(loginform, width=600)
85 MidLoginForm.pack(side=TOP, pady=50)
86 lbl_username = Label(MidLoginForm, text="Username:", font=('arial', 25), bd=18)
87 lbl_username.grid(row=0)
88 lbl_password = Label(MidLoginForm, text="Password:", font=('arial', 25), bd=18)
89 lbl_password.grid(row=1)
90 lbl_result = Label(MidLoginForm, text="", font=('arial', 18))
91 lbl_result.grid(row=3, columnspan=2)
92 username = Entry(MidLoginForm, textvariable=USERNAME, font=('arial', 25), width=15)
93 username.grid(row=0, column=1)
94 password = Entry(MidLoginForm, textvariable=PASSWORD, font=('arial', 25), width=15, show="*")
95 password.grid(row=1, column=1)
96 btn_login = Button(MidLoginForm, text="Login", font=('arial', 18), width=30, command=Login)
97 btn_login.grid(row=2, columnspan=2, pady=20)
98 btn_login.bind('<Return>', Login)
99
100
101def Home():
102 global Home
103 Home = Tk()
104 Home.title("Vehicle Inventory System/Home")
105 width = 1024
106 height = 520
107 screen_width = Home.winfo_screenwidth()
108 screen_height = Home.winfo_screenheight()
109 x = (screen_width / 2) - (width / 2)
110 y = (screen_height / 2) - (height / 2)
111 Home.geometry("%dx%d+%d+%d" % (width, height, x, y))
112 Home.resizable(0, 0)
113 Title = Frame(Home, bd=1, relief=SOLID)
114 Title.pack(pady=10)
115 lbl_display = Label(Title, text="Vehicle Inventory System", font=('arial', 45))
116 lbl_display.pack()
117 menubar = Menu(Home)
118 filemenu = Menu(menubar, tearoff=0)
119 filemenu2 = Menu(menubar, tearoff=0)
120 filemenu.add_command(label="Logout", command=Logout)
121 filemenu.add_command(label="Exit", command=Exit2)
122 filemenu2.add_command(label="Add new", command=ShowAddNew)
123 filemenu2.add_command(label="View", command=ShowView)
124 menubar.add_cascade(label="Account", menu=filemenu)
125 menubar.add_cascade(label="Inventory", menu=filemenu2)
126 Home.config(menu=menubar)
127 Home.config(bg="#6666ff")
128
129
130def ShowAddNew():
131 global addnewform
132 addnewform = Toplevel()
133 addnewform.title("Vehicle Inventory System/Add new")
134 width = 600
135 height = 700
136 screen_width = Home.winfo_screenwidth()
137 screen_height = Home.winfo_screenheight()
138 x = (screen_width / 2) - (width / 2)
139 y = (screen_height / 2) - (height / 2)
140 addnewform.geometry("%dx%d+%d+%d" % (width, height, x, y))
141 addnewform.resizable(0, 0)
142 AddNewForm()
143
144
145def AddNewForm():
146 TopAddNew = Frame(addnewform, width=600, height=300, bd=1, relief=SOLID)
147 TopAddNew.pack(side=TOP, pady=20)
148 lbl_text = Label(TopAddNew, text="Add New Vehicle", font=('arial', 18), width=600)
149 lbl_text.pack(fill=X)
150 MidAddNew = Frame(addnewform, height=600, width=700)
151 MidAddNew.pack(side=TOP, pady=50)
152 lbl_vehiclename = Label(MidAddNew, text="Vehicle Name:", font=('arial', 15), bd=10)
153 lbl_vehiclename.grid(row=0, sticky=W)
154 lbl_vehiclemodel = Label(MidAddNew, text="Vehicle Model:", font=('arial', 15), bd=10)
155 lbl_vehiclemodel.grid(row=1, sticky=W)
156 lbl_vehicleyear = Label(MidAddNew, text="Vehicle Year:", font=('arial', 15), bd=10)
157 lbl_vehicleyear.grid(row=2, sticky=W)
158 lbl_color = Label(MidAddNew, text="Vehicle Color:", font=('arial', 15), bd=10)
159 lbl_color.grid(row=3, sticky=W)
160 lbl_vehiclemileage = Label(MidAddNew, text="Vehicle Mileage:", font=('arial', 15), bd=10)
161 lbl_vehiclemileage.grid(row=4, sticky=W)
162 lbl_qty = Label(MidAddNew, text="Vehicle Quantity:", font=('arial', 15), bd=10)
163 lbl_qty.grid(row=5, sticky=W)
164 lbl_price = Label(MidAddNew, text="Vehicle Price:", font=('arial', 15), bd=10)
165 lbl_price.grid(row=6, sticky=W)
166
167 vehiclename = Entry(MidAddNew, textvariable=VEHICLE_NAME, font=('arial', 25), width=15)
168 vehiclename.grid(row=0, column=1)
169 vehiclemodel = Entry(MidAddNew, textvariable=VEHICLE_MODEL, font=('arial', 25), width=15)
170 vehiclemodel.grid(row=1, column=1)
171 vehicleyear = Entry(MidAddNew, textvariable=VEHICLE_YEAR, font=('arial', 25), width=15)
172 vehicleyear.grid(row=2, column=1)
173 vehiclecolor = Entry(MidAddNew, textvariable=VEHICLE_COLOR, font=('arial', 25), width=15)
174 vehiclecolor.grid(row=3, column=1)
175 vehiclemileage = Entry(MidAddNew, textvariable=VEHICLE_MILEAGE, font=('arial', 25), width=15)
176 vehiclemileage.grid(row=4, column=1)
177 vehicleqty = Entry(MidAddNew, textvariable=VEHICLE_QTY, font=('arial', 25), width=15)
178 vehicleqty.grid(row=5, column=1)
179 vehicleprice = Entry(MidAddNew, textvariable=VEHICLE_PRICE, font=('arial', 25), width=15)
180 vehicleprice.grid(row=6, column=1)
181 btn_add = Button(MidAddNew, text="Save", font=('arial', 18), width=15, bg="#009ACD", command=AddNew)
182 btn_add.grid(row=7, columnspan=2, pady=20)
183
184
185def AddNew():
186 Database()
187 cursor.execute("INSERT INTO `vehicle` (vehicle_name, vehicle_model, vehicle_year, vehicle_color, vehicle_mileage, vehicle_qty, vehicle_price) VALUES(?, ?, ?, ?, ?, ?, ?)",
188 (str(VEHICLE_NAME.get()), str(VEHICLE_MODEL.get()), int(VEHICLE_YEAR.get()), str(VEHICLE_COLOR.get()), int(VEHICLE_MILEAGE.get()), int(VEHICLE_QTY.get()), int(VEHICLE_PRICE.get())))
189 conn.commit()
190 VEHICLE_NAME.set("")
191 VEHICLE_MODEL.set("")
192 VEHICLE_YEAR.set("")
193 VEHICLE_COLOR.set("")
194 VEHICLE_MILEAGE.set("")
195 VEHICLE_PRICE.set("")
196 VEHICLE_QTY.set("")
197 cursor.close()
198 conn.close()
199
200
201def ViewForm():
202 global tree
203 TopViewForm = Frame(viewform, width=600, bd=1, relief=SOLID)
204 TopViewForm.pack(side=TOP, fill=X)
205 LeftViewForm = Frame(viewform, width=600)
206 LeftViewForm.pack(side=LEFT, fill=Y)
207 MidViewForm = Frame(viewform, width=600)
208 MidViewForm.pack(side=RIGHT)
209 lbl_text = Label(TopViewForm, text="View Vehicles", font=('arial', 18), width=600)
210 lbl_text.pack(fill=X)
211 lbl_txtsearch = Label(LeftViewForm, text="Search", font=('arial', 15))
212 lbl_txtsearch.pack(side=TOP, anchor=W)
213 search = Entry(LeftViewForm, textvariable=SEARCH, font=('arial', 15), width=10)
214 search.pack(side=TOP, padx=10, fill=X)
215 btn_search = Button(LeftViewForm, text="Search", command=Search)
216 btn_search.pack(side=TOP, padx=10, pady=10, fill=X)
217 btn_reset = Button(LeftViewForm, text="Reset", command=Reset)
218 btn_reset.pack(side=TOP, padx=10, pady=10, fill=X)
219 btn_delete = Button(LeftViewForm, text="Delete", command=Delete)
220 btn_delete.pack(side=TOP, padx=10, pady=10, fill=X)
221 scrollbarx = Scrollbar(MidViewForm, orient=HORIZONTAL)
222 scrollbary = Scrollbar(MidViewForm, orient=VERTICAL)
223 tree = ttk.Treeview(MidViewForm, columns=("VehicleID", "Vehicle Name", "Vehicle Model", "Vehicle Year", "Vehicle Color", "Vehicle Mileage", "Vehicle Qty", "Vehicle Price"),
224 selectmode="extended", height=100, yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
225 scrollbary.config(command=tree.yview)
226 scrollbary.pack(side=RIGHT, fill=Y)
227 scrollbarx.config(command=tree.xview)
228 scrollbarx.pack(side=BOTTOM, fill=X)
229 tree.heading('VehicleID', text="VehicleID", anchor=W)
230 tree.heading('Vehicle Name', text="Vehicle Name", anchor=W)
231 tree.heading('Vehicle Model', text="Vehicle Model", anchor=W)
232 tree.heading('Vehicle Year', text="Vehicle Year", anchor=W)
233 tree.heading('Vehicle Color', text="Vehicle Color", anchor=W)
234 tree.heading('Vehicle Mileage', text="Vehicle Mileage", anchor=W)
235 tree.heading('Vehicle Qty', text="Vehicle Qty", anchor=W)
236 tree.heading('Vehicle Price', text="Vehicle Price", anchor=W)
237 tree.column('#0', stretch=NO, minwidth=0, width=0)
238 tree.column('#1', stretch=NO, minwidth=0, width=0)
239 tree.column('#2', stretch=NO, minwidth=0, width=100)
240 tree.column('#3', stretch=NO, minwidth=0, width=100)
241 tree.column('#4', stretch=NO, minwidth=0, width=100)
242 tree.column('#5', stretch=NO, minwidth=0, width=100)
243 tree.column('#6', stretch=NO, minwidth=0, width=100)
244 tree.column('#7', stretch=NO, minwidth=0, width=100)
245 tree.pack()
246 DisplayData()
247
248
249def DisplayData():
250 Database()
251 cursor.execute("SELECT * FROM `vehicle`")
252 fetch = cursor.fetchall()
253 for data in fetch:
254 tree.insert('', 'end', values=(data))
255 cursor.close()
256 conn.close()
257
258
259def Search():
260 if SEARCH.get() != "":
261 tree.delete(*tree.get_children())
262 Database()
263 cursor.execute("SELECT * FROM `vehicle` WHERE `vehicle_name` LIKE ?", ('%' + str(SEARCH.get()) + '%',))
264 fetch = cursor.fetchall()
265 for data in fetch:
266 tree.insert('', 'end', values=(data))
267 cursor.close()
268 conn.close()
269
270
271def Reset():
272 tree.delete(*tree.get_children())
273 DisplayData()
274 SEARCH.set("")
275
276
277def Delete():
278 if not tree.selection():
279 result = tkMessageBox.showerror('Vehicle Inventory System', 'You did not select a vehicle to delete', icon="warning")
280 else:
281 result = tkMessageBox.askquestion('Vehicle Inventory System', 'Are you sure you want to delete this record?',
282 icon="warning")
283 if result == 'yes':
284 curItem = tree.focus()
285 contents = (tree.item(curItem))
286 selecteditem = contents['values']
287 tree.delete(curItem)
288 Database()
289 cursor.execute("DELETE FROM `vehicle` WHERE `vehicle_id` = %d" % selecteditem[0])
290 conn.commit()
291 cursor.close()
292 conn.close()
293
294
295def ShowView():
296 global viewform
297 viewform = Toplevel()
298 viewform.title("Vehicle Inventory System/View Vehicle")
299 width = 600
300 height = 400
301 screen_width = Home.winfo_screenwidth()
302 screen_height = Home.winfo_screenheight()
303 x = (screen_width / 2) - (width / 2)
304 y = (screen_height / 2) - (height / 2)
305 viewform.geometry("%dx%d+%d+%d" % (width, height, x, y))
306 viewform.resizable(0, 0)
307 ViewForm()
308
309
310def Logout():
311 result = tkMessageBox.askquestion('Vehicle Inventory System', 'Are you sure you want to logout?', icon="warning")
312 if result == 'yes':
313 admin_id = ""
314 root.deiconify()
315 Home.destroy()
316
317
318def Login(event=None):
319 global admin_id
320 Database()
321 if USERNAME.get == "" or PASSWORD.get() == "":
322 lbl_result.config(text="Please complete the required field!", fg="red")
323 else:
324 cursor.execute("SELECT * FROM `admin` WHERE `username` = ? AND `password` = ?",
325 (USERNAME.get(), PASSWORD.get()))
326 if cursor.fetchone() is not None:
327 cursor.execute("SELECT * FROM `admin` WHERE `username` = ? AND `password` = ?",
328 (USERNAME.get(), PASSWORD.get()))
329 data = cursor.fetchone()
330 admin_id = data[0]
331 USERNAME.set("")
332 PASSWORD.set("")
333 lbl_result.config(text="")
334 ShowHome()
335 else:
336 lbl_result.config(text="Invalid username or password", fg="red")
337 USERNAME.set("")
338 PASSWORD.set("")
339 cursor.close()
340 conn.close()
341
342
343def ShowHome():
344 root.withdraw()
345 Home()
346 loginform.destroy()
347
348
349# ========================================MENUBAR WIDGETS==================================
350menubar = Menu(root)
351filemenu = Menu(menubar, tearoff=0)
352filemenu.add_command(label="Account", command=ShowLoginForm)
353filemenu.add_command(label="Exit", command=Exit)
354menubar.add_cascade(label="File", menu=filemenu)
355root.config(menu=menubar)
356
357# ========================================FRAME============================================
358Title = Frame(root, bd=1, relief=SOLID)
359Title.pack(pady=10)
360
361# ========================================LABEL WIDGET=====================================
362lbl_display = Label(Title, text="Vehicle Inventory System", font=('arial', 45))
363lbl_display.pack()
364
365# ========================================INITIALIZATION===================================
366if __name__ == '__main__':
367 root.mainloop()