Advertisement
Temitop

Untitled

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