Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #========STEP=========
- #Equipment Inventory Program is to be used for listing and organizing needed equipment for transports.
- #User friendly program allows alterations to SQL database.
- #------Importing Tkinter and SQL database
- from tkinter import *
- import tkinter.messagebox as tkMessageBox
- import sqlite3
- import tkinter.ttk as ttk
- mainUser = Tk()
- mainUser.title("Python - Mobile ECMO Equipment Inventory System")
- #------Main frame for the Application.
- #Defining sturcture and coordinates
- width = 1024
- height = 720
- screen_width = mainUser.winfo_screenmmwidth()
- screen_height = mainUser.winfo_screenheight()
- x = (screen_width/2) - (width/2)
- y = (screen_height) - (height/2)
- #------Tinkter window coordinate
- mainUser.geometry("%dx%d+%d+%d" % (width, height, x, y))
- mainUser.resizable(0, 0)
- mainUser.config(bg="#99ff99")
- #------Layout design
- #Frame
- Title = Frame(mainUser, bd=1, relief=SOLID)
- Title.pack(pady=10)
- #Menu bar
- menubar = Menu(mainUser)
- filemenu = Menu(menubar, tearoff=0)
- filemenu.add_command(label="User: ", command=ShowLoginForm)
- filemenu.add_command(label="Exit", command=Exit)
- menubar.add_cascade(label="Forms", menu=filemenu)
- mainUser.config(menu=menubar)
- #Label Big
- lbl_display = Label(Title, text="Primary ECMO transport Inventory", font=('Helvetica', 40))
- lbl_display.pack()
- #------Database Connection
- def Database():
- global conn, cursor
- conn = sqlite3.connect("ecmoInventory.db")
- cursor = conn.cursor()
- cursor.execute("CREATE TABLE IF NOT EXISTS `admin` (admin_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT, password TEXT)")
- cursor.execute("CREATE TABLE IF NOT EXISTS `item` (item_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, item_name TEXT, itemt_qty TEXT, item_useorder TEXT)")
- cursor.execute("SELECT * FROM `admin` WHERE `username` = 'admin' AND `password` = 'admin'")
- if cursor.fetchone() is None:
- cursor.execute("INSERT INTO`admin` (username, password) VALUES('admin', 'admin')")
- conn.commit()
- #==================== Main Function ====================
- #Exit
- def Exit():
- result = tkMessageBox.askquestion('Primary ECMO transport Inventory', 'Are you sure you want to exit?', icon="warning")
- if result == 'yes':
- mainUser.destroy()
- exit()
- def Exit2():
- result = tkMessageBox.askquestion('Primary ECMO transport Inventory', 'Are you sure you want to exit?', icon="warning")
- if result == 'yes':
- Home.destroy()
- exit()
- #Login
- def ShowLoginForm():
- global loginform
- loginform = Toplevel()
- loginform.title("Primary ECMO transport Inventory/Account Login")
- width = 600
- height = 500
- screen_width = mainUser.winfo_screenwidth()
- screen_height = mainUser.winfo_screenheight()
- x = (screen_width/2) - (width/2)
- y = (screen_height/2) - (height/2)
- loginform.resizable(0, 0)
- loginform.geometry("%dx%d+%d+%d" % (width, height, x, y))
- LoginForm()
- def LoginForm():
- global lbl_result
- TopLoginForm = Frame(loginform, width=600, height=100, bd=1, relief=SOLID)
- TopLoginForm.pack(side=TOP, pady=20)
- lbl_text = Label(TopLoginForm, text="Main Login", font=('arial', 18), width=600)
- lbl_text.pack(fill=X)
- MidLoginForm = Frame(loginform, width=600)
- MidLoginForm.pack(side=TOP, pady=50)
- lbl_username = Label(MidLoginForm, text="Username:", font=('arial', 25), bd=18)
- lbl_username.grid(row=0)
- lbl_password = Label(MidLoginForm, text="Password:", font=('arial', 25), bd=18)
- lbl_password.grid(row=1)
- lbl_result = Label(MidLoginForm, text="", font=('arial', 18))
- lbl_result.grid(row=3, columnspan=2)
- username = Entry(MidLoginForm, textvariable=USERNAME, font=('arial', 25), width=15)
- username.grid(row=0, column=1)
- password = Entry(MidLoginForm, textvariable=PASSWORD, font=('arial', 25), width=15, show="*")
- password.grid(row=1, column=1)
- btn_login = Button(MidLoginForm, text="Login", font=('arial', 18), width=30, command=Login)
- btn_login.grid(row=2, columnspan=2, pady=20)
- btn_login.bind('<Return>', Login)
- #Home
- def Home():
- global Home
- Home = Tk()
- Home.title("Primary ECMO transport Inventory/Home")
- width = 1024
- height = 720
- screen_width = Home.winfo_screenwidth()
- screen_height = Home.winfo_screenheight()
- x = (screen_width/2) - (width/2)
- y = (screen_height/2) - (height/2)
- Home.geometry("%dx%d+%d+%d" % (width, height, x, y))
- Home.resizable(0, 0)
- Title = Frame(Home, bd=1, relief=SOLID)
- Title.pack(pady=10)
- lbl_display = Label(Title, text="Simple Inventory System", font=('arial', 45))
- lbl_display.pack()
- menubar = Menu(Home)
- filemenu = Menu(menubar, tearoff=0)
- filemenu2 = Menu(menubar, tearoff=0)
- filemenu.add_command(label="Logout", command=Logout)
- filemenu.add_command(label="Exit", command=Exit2)
- filemenu2.add_command(label="Add new", command=ShowAddNew)
- filemenu2.add_command(label="View", command=ShowView)
- menubar.add_cascade(label="Account", menu=filemenu)
- menubar.add_cascade(label="Inventory", menu=filemenu2)
- Home.config(menu=menubar)
- Home.config(bg="#99ff99")
- #Forms
- def ShowAddNew():
- global addnewform
- addnewform = Toplevel()
- addnewform.title("Primary ECMO transport Inventory/Add new")
- width = 600
- height = 500
- screen_width = Home.winfo_screenwidth()
- screen_height = Home.winfo_screenheight()
- x = (screen_width/2) - (width/2)
- y = (screen_height/2) - (height/2)
- addnewform.geometry("%dx%d+%d+%d" % (width, height, x, y))
- addnewform.resizable(0, 0)
- AddNewForm()
- def AddNewForm():
- TopAddNew = Frame(addnewform, width=600, height=100, bd=1, relief=SOLID)
- TopAddNew.pack(side=TOP, pady=20)
- lbl_text = Label(TopAddNew, text="Add New Item", font=('arial', 18), width=600)
- lbl_text.pack(fill=X)
- MidAddNew = Frame(addnewform, width=600)
- MidAddNew.pack(side=TOP, pady=50)
- lbl_itemname = Label(MidAddNew, text="Item name:", font=('arial', 25), bd=10)
- lbl_itemname.grid(row=0, sticky=W)
- lbl_qty = Label(MidAddNew, text="Item Quantity:", font=('arial', 25), bd=10)
- lbl_qty.grid(row=1, sticky=W)
- lbl_useor = Label(MidAddNew, text="Item Use Order:", font=('arial', 25), bd=10)
- lbl_useor.grid(row=2, sticky=W)
- itemname = Entry(MidAddNew, textvariable=ITEM_NAME, font=('arial', 25), width=15)
- itemname.grid(row=0, column=1)
- itemqty = Entry(MidAddNew, textvariable=ITEM_QTY, font=('arial', 25), width=15)
- itemqty.grid(row=1, column=1)
- itemuseor = Entry(MidAddNew, textvariable=ITEM_USEOR, font=('arial', 25), width=15)
- itemuseor.grid(row=2, column=1)
- btn_add = Button(MidAddNew, text="Save", font=('arial', 18), width=30, bg="#009ACD", command=AddNew)
- btn_add.grid(row=3, columnspan=2, pady=20)
- #Database
- def AddNew():
- Database()
- cursor.execute("INSERT INTO `item` (item_name, item_qty, item_useor) VALUES(?, ?, ?)", (str(ITEM_NAME.get()), int(ITEM_QTY.get()), int(ITEM_USEOR.get())))
- conn.commit()
- ITEM_NAME.set("")
- ITEM_USEOR.set("")
- ITEM_QTY.set("")
- cursor.close()
- conn.close()
- #Form Illustrating part
- def viewForm():
- global tree
- TopViewForm = Frame(viewform, width=600, bd=1, relief=SOLID)
- TopViewForm.pack(side=TOP, fill=X)
- LeftViewForm = Frame(viewform, width=600)
- LeftViewForm.pack(side=LEFT, fill=Y)
- MidViewForm = Frame(viewform, width=600)
- MidViewForm.pack(side=RIGHT)
- lbl_text = Label(TopViewForm, text="View Items", font=('arial', 18), width=600)
- lbl_text.pack(fill=X)
- lbl_txtsearch = Label(LeftViewForm, text="Search", font=('arial', 15))
- lbl_txtsearch.pack(side=TOP, anchor=W)
- search = Entry(LeftViewForm, textvariable=SEARCH, font=('arial', 15), width=10)
- search.pack(side=TOP, padx=10, fill=X)
- btn_search = Button(LeftViewForm, text="Search", command=Search)
- btn_search.pack(side=TOP, padx=10, pady=10, fill=X)
- btn_reset = Button(LeftViewForm, text="Reset", command=Reset)
- btn_reset.pack(side=TOP, padx=10, pady=10, fill=X)
- btn_delete = Button(LeftViewForm, text="Delete", command=Delete)
- btn_delete.pack(side=TOP, padx=10, pady=10, fill=X)
- scrollbarx = Scrollbar(MidViewForm, orient=HORIZONTAL)
- scrollbary = Scrollbar(MidViewForm, orient=VERTICAL)
- tree = ttk.Treeview(MidViewForm, columns=("ItemID", "Item Name", "Item Qty", "Item Use Order"), selectmode="extended", height=100, yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
- scrollbary.config(command=tree.yview)
- scrollbary.pack(side=RIGHT, fill=Y)
- scrollbarx.config(command=tree.xview)
- scrollbarx.pack(side=BOTTOM, fill=X)
- tree.heading('Item ID', text="ItemID",anchor=W)
- tree.heading('Item Name', text="Item Name",anchor=W)
- tree.heading('Item Qty', text="Item Qty",anchor=W)
- tree.heading('Item Use Order', text="Item Use Order",anchor=W)
- tree.column('#0', stretch=NO, minwidth=0, width=0)
- tree.column('#1', stretch=NO, minwidth=0, width=0)
- tree.column('#2', stretch=NO, minwidth=0, width=200)
- tree.column('#3', stretch=NO, minwidth=0, width=120)
- tree.column('#4', stretch=NO, minwidth=0, width=120)
- tree.pack()
- DisplayData()
- # Data display
- def DisplayData():
- Database()
- cursor.execute("SELECT * FROM `product`")
- fetch = cursor.fetchall()
- for data in fetch:
- tree.insert('', 'end', values=(data))
- cursor.close()
- conn.close()
- #Search items
- def Search():
- if SEARCH.get() != "":
- tree.delete(*tree.get_children())
- Database()
- cursor.execute("SELECT * FROM `product` WHERE `product_name` LIKE ?", ('%' + str(SEARCH.get()) + '%',))
- fetch = cursor.fetchall()
- for data in fetch:
- tree.insert('', 'end', values=(data))
- cursor.close()
- conn.close()
- def Reset():
- tree.delete(*tree.get_children())
- DisplayData()
- SEARCH.set("")
- #Delete items
- def Delete():
- if not tree.selection():
- print("ERROR")
- else:
- result = tkMessageBox.askquestion('Primary ECMO transport Inventory', 'Are you sure you want to delete this item?', icon="warning")
- if result == 'yes':
- curItem = tree.focus()
- contents =(tree.item(curItem))
- selecteditem = contents['values']
- tree.delete(curItem)
- Database()
- cursor.execute("DELETE FROM `product` WHERE `product_id` = %d" % selecteditem[0])
- conn.commit()
- cursor.close()
- conn.close()
- def ShowView():
- global viewform
- viewform = Toplevel()
- viewform.title("Primary ECMO transport Inventory/View Product")
- width = 600
- height = 400
- screen_width = Home.winfo_screenwidth()
- screen_height = Home.winfo_screenheight()
- x = (screen_width/2) - (width/2)
- y = (screen_height/2) - (height/2)
- viewform.geometry("%dx%d+%d+%d" % (width, height, x, y))
- viewform.resizable(0, 0)
- ViewForm()
- #Log out
- def Logout():
- result = tkMessageBox.askquestion('Primary ECMO transport Inventory', 'Are you sure you want to log out?', icon="warning")
- if result == 'yes':
- admin_id = ""
- mainUser.deiconify()
- Home.destroy()
- #Log in
- def Login(event = None):
- global admin_id
- Database()
- if USERNAME.get == "" or PASSWORD.get() == "":
- lbl_result.config(text="Fill in the required field", fg="red")
- else:
- cursor.execute("SELECT * FROM `admin` WHERE `username` = ? AND `password` = ?", (USERNAME.get(), PASSWORD.get()))
- if cursor.fetchone() is not None:
- cursor.execute("SELECT * FROM `admin` WHERE `username` = ? AND `password` = ?", (USERNAME.get(), PASSWORD.get()))
- data = cursor.fetchone()
- admin_id = data[0]
- USERNAME.set("")
- PASSWORD.set("")
- lbl_result.config(text="")
- ShowHome()
- else:
- lbl_result.config(text="Invalid username or password", fg="red")
- USERNAME.set("")
- PASSWORD.set("")
- cursor.close()
- conn.close()
- def ShowHome():
- mainUser.withdraw()
- Home()
- loginform.destroy()
Add Comment
Please, Sign In to add comment