Guest User

Untitled

a guest
Feb 23rd, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.84 KB | None | 0 0
  1. #========STEP=========
  2. #Equipment Inventory Program is to be used for listing and organizing needed equipment for transports.
  3. #User friendly program allows alterations to SQL database.
  4.  
  5. #------Importing Tkinter and SQL database
  6.  
  7. from tkinter import *
  8. import tkinter.messagebox as tkMessageBox
  9. import sqlite3
  10. import tkinter.ttk as ttk
  11.  
  12. mainUser = Tk()
  13. mainUser.title("Python - Mobile ECMO Equipment Inventory System")
  14.  
  15. #------Main frame for the Application.
  16. #Defining sturcture and coordinates
  17.  
  18. width = 1024
  19. height = 720
  20. screen_width = mainUser.winfo_screenmmwidth()
  21. screen_height = mainUser.winfo_screenheight()
  22.  
  23. x = (screen_width/2) - (width/2)
  24. y = (screen_height) - (height/2)
  25.  
  26. #------Tinkter window coordinate
  27.  
  28. mainUser.geometry("%dx%d+%d+%d" % (width, height, x, y))
  29. mainUser.resizable(0, 0)
  30. mainUser.config(bg="#99ff99")
  31.  
  32. #------Layout design
  33. #Frame
  34.  
  35. Title = Frame(mainUser, bd=1, relief=SOLID)
  36. Title.pack(pady=10)
  37.  
  38. #Menu bar
  39. menubar = Menu(mainUser)
  40. filemenu = Menu(menubar, tearoff=0)
  41. filemenu.add_command(label="User: ", command=ShowLoginForm)
  42. filemenu.add_command(label="Exit", command=Exit)
  43. menubar.add_cascade(label="Forms", menu=filemenu)
  44. mainUser.config(menu=menubar)
  45.  
  46.  
  47. #Label Big
  48. lbl_display = Label(Title, text="Primary ECMO transport Inventory", font=('Helvetica', 40))
  49. lbl_display.pack()
  50.  
  51. #------Database Connection
  52.  
  53. def Database():
  54. global conn, cursor
  55. conn = sqlite3.connect("ecmoInventory.db")
  56. cursor = conn.cursor()
  57. cursor.execute("CREATE TABLE IF NOT EXISTS `admin` (admin_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT, password TEXT)")
  58. 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)")
  59. cursor.execute("SELECT * FROM `admin` WHERE `username` = 'admin' AND `password` = 'admin'")
  60. if cursor.fetchone() is None:
  61. cursor.execute("INSERT INTO`admin` (username, password) VALUES('admin', 'admin')")
  62. conn.commit()
  63.  
  64. #==================== Main Function ====================
  65.  
  66. #Exit
  67.  
  68. def Exit():
  69. result = tkMessageBox.askquestion('Primary ECMO transport Inventory', 'Are you sure you want to exit?', icon="warning")
  70. if result == 'yes':
  71. mainUser.destroy()
  72. exit()
  73.  
  74. def Exit2():
  75. result = tkMessageBox.askquestion('Primary ECMO transport Inventory', 'Are you sure you want to exit?', icon="warning")
  76. if result == 'yes':
  77. Home.destroy()
  78. exit()
  79.  
  80. #Login
  81.  
  82. def ShowLoginForm():
  83. global loginform
  84. loginform = Toplevel()
  85. loginform.title("Primary ECMO transport Inventory/Account Login")
  86. width = 600
  87. height = 500
  88. screen_width = mainUser.winfo_screenwidth()
  89. screen_height = mainUser.winfo_screenheight()
  90. x = (screen_width/2) - (width/2)
  91. y = (screen_height/2) - (height/2)
  92. loginform.resizable(0, 0)
  93. loginform.geometry("%dx%d+%d+%d" % (width, height, x, y))
  94. LoginForm()
  95.  
  96. def LoginForm():
  97. global lbl_result
  98. TopLoginForm = Frame(loginform, width=600, height=100, bd=1, relief=SOLID)
  99. TopLoginForm.pack(side=TOP, pady=20)
  100. lbl_text = Label(TopLoginForm, text="Main Login", font=('arial', 18), width=600)
  101. lbl_text.pack(fill=X)
  102. MidLoginForm = Frame(loginform, width=600)
  103. MidLoginForm.pack(side=TOP, pady=50)
  104.  
  105. lbl_username = Label(MidLoginForm, text="Username:", font=('arial', 25), bd=18)
  106. lbl_username.grid(row=0)
  107. lbl_password = Label(MidLoginForm, text="Password:", font=('arial', 25), bd=18)
  108. lbl_password.grid(row=1)
  109. lbl_result = Label(MidLoginForm, text="", font=('arial', 18))
  110. lbl_result.grid(row=3, columnspan=2)
  111.  
  112. username = Entry(MidLoginForm, textvariable=USERNAME, font=('arial', 25), width=15)
  113. username.grid(row=0, column=1)
  114. password = Entry(MidLoginForm, textvariable=PASSWORD, font=('arial', 25), width=15, show="*")
  115. password.grid(row=1, column=1)
  116.  
  117. btn_login = Button(MidLoginForm, text="Login", font=('arial', 18), width=30, command=Login)
  118. btn_login.grid(row=2, columnspan=2, pady=20)
  119. btn_login.bind('<Return>', Login)
  120.  
  121. #Home
  122.  
  123. def Home():
  124. global Home
  125. Home = Tk()
  126. Home.title("Primary ECMO transport Inventory/Home")
  127. width = 1024
  128. height = 720
  129. screen_width = Home.winfo_screenwidth()
  130. screen_height = Home.winfo_screenheight()
  131. x = (screen_width/2) - (width/2)
  132. y = (screen_height/2) - (height/2)
  133. Home.geometry("%dx%d+%d+%d" % (width, height, x, y))
  134. Home.resizable(0, 0)
  135. Title = Frame(Home, bd=1, relief=SOLID)
  136. Title.pack(pady=10)
  137. lbl_display = Label(Title, text="Simple Inventory System", font=('arial', 45))
  138. lbl_display.pack()
  139.  
  140. menubar = Menu(Home)
  141. filemenu = Menu(menubar, tearoff=0)
  142. filemenu2 = Menu(menubar, tearoff=0)
  143. filemenu.add_command(label="Logout", command=Logout)
  144. filemenu.add_command(label="Exit", command=Exit2)
  145. filemenu2.add_command(label="Add new", command=ShowAddNew)
  146. filemenu2.add_command(label="View", command=ShowView)
  147. menubar.add_cascade(label="Account", menu=filemenu)
  148. menubar.add_cascade(label="Inventory", menu=filemenu2)
  149. Home.config(menu=menubar)
  150. Home.config(bg="#99ff99")
  151.  
  152. #Forms
  153.  
  154. def ShowAddNew():
  155. global addnewform
  156. addnewform = Toplevel()
  157. addnewform.title("Primary ECMO transport Inventory/Add new")
  158. width = 600
  159. height = 500
  160. screen_width = Home.winfo_screenwidth()
  161. screen_height = Home.winfo_screenheight()
  162. x = (screen_width/2) - (width/2)
  163. y = (screen_height/2) - (height/2)
  164. addnewform.geometry("%dx%d+%d+%d" % (width, height, x, y))
  165. addnewform.resizable(0, 0)
  166. AddNewForm()
  167.  
  168. def AddNewForm():
  169. TopAddNew = Frame(addnewform, width=600, height=100, bd=1, relief=SOLID)
  170. TopAddNew.pack(side=TOP, pady=20)
  171. lbl_text = Label(TopAddNew, text="Add New Item", font=('arial', 18), width=600)
  172. lbl_text.pack(fill=X)
  173. MidAddNew = Frame(addnewform, width=600)
  174. MidAddNew.pack(side=TOP, pady=50)
  175. lbl_itemname = Label(MidAddNew, text="Item name:", font=('arial', 25), bd=10)
  176. lbl_itemname.grid(row=0, sticky=W)
  177. lbl_qty = Label(MidAddNew, text="Item Quantity:", font=('arial', 25), bd=10)
  178. lbl_qty.grid(row=1, sticky=W)
  179. lbl_useor = Label(MidAddNew, text="Item Use Order:", font=('arial', 25), bd=10)
  180. lbl_useor.grid(row=2, sticky=W)
  181. itemname = Entry(MidAddNew, textvariable=ITEM_NAME, font=('arial', 25), width=15)
  182. itemname.grid(row=0, column=1)
  183. itemqty = Entry(MidAddNew, textvariable=ITEM_QTY, font=('arial', 25), width=15)
  184. itemqty.grid(row=1, column=1)
  185. itemuseor = Entry(MidAddNew, textvariable=ITEM_USEOR, font=('arial', 25), width=15)
  186. itemuseor.grid(row=2, column=1)
  187. btn_add = Button(MidAddNew, text="Save", font=('arial', 18), width=30, bg="#009ACD", command=AddNew)
  188. btn_add.grid(row=3, columnspan=2, pady=20)
  189.  
  190. #Database
  191.  
  192. def AddNew():
  193. Database()
  194. cursor.execute("INSERT INTO `item` (item_name, item_qty, item_useor) VALUES(?, ?, ?)", (str(ITEM_NAME.get()), int(ITEM_QTY.get()), int(ITEM_USEOR.get())))
  195. conn.commit()
  196. ITEM_NAME.set("")
  197. ITEM_USEOR.set("")
  198. ITEM_QTY.set("")
  199. cursor.close()
  200. conn.close()
  201.  
  202. #Form Illustrating part
  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.  
  213. lbl_text = Label(TopViewForm, text="View Items", font=('arial', 18), width=600)
  214. lbl_text.pack(fill=X)
  215. lbl_txtsearch = Label(LeftViewForm, text="Search", font=('arial', 15))
  216. lbl_txtsearch.pack(side=TOP, anchor=W)
  217. search = Entry(LeftViewForm, textvariable=SEARCH, font=('arial', 15), width=10)
  218. search.pack(side=TOP, padx=10, fill=X)
  219. btn_search = Button(LeftViewForm, text="Search", command=Search)
  220. btn_search.pack(side=TOP, padx=10, pady=10, fill=X)
  221. btn_reset = Button(LeftViewForm, text="Reset", command=Reset)
  222. btn_reset.pack(side=TOP, padx=10, pady=10, fill=X)
  223. btn_delete = Button(LeftViewForm, text="Delete", command=Delete)
  224. btn_delete.pack(side=TOP, padx=10, pady=10, fill=X)
  225. scrollbarx = Scrollbar(MidViewForm, orient=HORIZONTAL)
  226. scrollbary = Scrollbar(MidViewForm, orient=VERTICAL)
  227. tree = ttk.Treeview(MidViewForm, columns=("ItemID", "Item Name", "Item Qty", "Item Use Order"), 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('Item ID', text="ItemID",anchor=W)
  233. tree.heading('Item Name', text="Item Name",anchor=W)
  234. tree.heading('Item Qty', text="Item Qty",anchor=W)
  235. tree.heading('Item Use Order', text="Item Use Order",anchor=W)
  236. tree.column('#0', stretch=NO, minwidth=0, width=0)
  237. tree.column('#1', stretch=NO, minwidth=0, width=0)
  238. tree.column('#2', stretch=NO, minwidth=0, width=200)
  239. tree.column('#3', stretch=NO, minwidth=0, width=120)
  240. tree.column('#4', stretch=NO, minwidth=0, width=120)
  241. tree.pack()
  242. DisplayData()
  243.  
  244. # Data display
  245.  
  246. def DisplayData():
  247. Database()
  248. cursor.execute("SELECT * FROM `product`")
  249. fetch = cursor.fetchall()
  250. for data in fetch:
  251. tree.insert('', 'end', values=(data))
  252. cursor.close()
  253. conn.close()
  254.  
  255. #Search items
  256.  
  257. def Search():
  258. if SEARCH.get() != "":
  259. tree.delete(*tree.get_children())
  260. Database()
  261. cursor.execute("SELECT * FROM `product` WHERE `product_name` LIKE ?", ('%' + str(SEARCH.get()) + '%',))
  262. fetch = cursor.fetchall()
  263. for data in fetch:
  264. tree.insert('', 'end', values=(data))
  265. cursor.close()
  266. conn.close()
  267.  
  268. def Reset():
  269. tree.delete(*tree.get_children())
  270. DisplayData()
  271. SEARCH.set("")
  272.  
  273. #Delete items
  274.  
  275. def Delete():
  276. if not tree.selection():
  277. print("ERROR")
  278. else:
  279. result = tkMessageBox.askquestion('Primary ECMO transport Inventory', 'Are you sure you want to delete this item?', icon="warning")
  280. if result == 'yes':
  281. curItem = tree.focus()
  282. contents =(tree.item(curItem))
  283. selecteditem = contents['values']
  284. tree.delete(curItem)
  285. Database()
  286. cursor.execute("DELETE FROM `product` WHERE `product_id` = %d" % selecteditem[0])
  287. conn.commit()
  288. cursor.close()
  289. conn.close()
  290.  
  291. def ShowView():
  292. global viewform
  293. viewform = Toplevel()
  294. viewform.title("Primary ECMO transport Inventory/View Product")
  295. width = 600
  296. height = 400
  297. screen_width = Home.winfo_screenwidth()
  298. screen_height = Home.winfo_screenheight()
  299. x = (screen_width/2) - (width/2)
  300. y = (screen_height/2) - (height/2)
  301. viewform.geometry("%dx%d+%d+%d" % (width, height, x, y))
  302. viewform.resizable(0, 0)
  303. ViewForm()
  304.  
  305. #Log out
  306.  
  307. def Logout():
  308. result = tkMessageBox.askquestion('Primary ECMO transport Inventory', 'Are you sure you want to log out?', icon="warning")
  309. if result == 'yes':
  310. admin_id = ""
  311. mainUser.deiconify()
  312. Home.destroy()
  313.  
  314. #Log in
  315.  
  316. def Login(event = None):
  317. global admin_id
  318. Database()
  319. if USERNAME.get == "" or PASSWORD.get() == "":
  320. lbl_result.config(text="Fill in the required field", fg="red")
  321. else:
  322. cursor.execute("SELECT * FROM `admin` WHERE `username` = ? AND `password` = ?", (USERNAME.get(), PASSWORD.get()))
  323. if cursor.fetchone() is not None:
  324. cursor.execute("SELECT * FROM `admin` WHERE `username` = ? AND `password` = ?", (USERNAME.get(), PASSWORD.get()))
  325. data = cursor.fetchone()
  326. admin_id = data[0]
  327. USERNAME.set("")
  328. PASSWORD.set("")
  329. lbl_result.config(text="")
  330. ShowHome()
  331. else:
  332. lbl_result.config(text="Invalid username or password", fg="red")
  333. USERNAME.set("")
  334. PASSWORD.set("")
  335. cursor.close()
  336. conn.close()
  337.  
  338. def ShowHome():
  339. mainUser.withdraw()
  340. Home()
  341. loginform.destroy()
Add Comment
Please, Sign In to add comment