Python: Simple Inventory System

Submitted by: 
Language: 
Visitors have accessed this post 15531 times.

In this tutorial we will create a Simple Inventory System Using Python / SQLite. Python is a computer programming language that lets work faster and convenient because of its user - friendly environment. Python supports packages and modules, which encourage a developer to program in a modularity and reusable way.. So let's now do the coding.

Getting started
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Installing SQLite Browser
After you installed Python, we will now then install the SQLite, here's the link for the DB Browser for SQLite http://sqlitebrowser.org/.

Importing Modules
After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python.

Then copy code that I provided below and paste it inside the IDLE text editor

  1. from tkinter import *
  2. import tkinter.messagebox as tkMessageBox
  3. import sqlite3
  4. import tkinter.ttk as ttk

Setting up the Main Frame
After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor

  1. root = Tk()
  2. root.title("Python: Simple Inventory System")
  3.  
  4. width = 1024
  5. height = 720
  6. screen_width = root.winfo_screenwidth()
  7. screen_height = root.winfo_screenheight()
  8. x = (screen_width/2) - (width/2)
  9. y = (screen_height/2) - (height/2)
  10. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  11. root.resizable(0, 0)
  12. root.config(bg="#99ff99")

Designing the Layout
After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.

  1. #========================================MENUBAR WIDGETS==================================
  2. menubar = Menu(root)
  3. filemenu = Menu(menubar, tearoff=0)
  4. filemenu.add_command(label="Account", command=ShowLoginForm)
  5. filemenu.add_command(label="Exit", command=Exit)
  6. menubar.add_cascade(label="File", menu=filemenu)
  7. root.config(menu=menubar)
  8.  
  9. #========================================FRAME============================================
  10. Title = Frame(root, bd=1, relief=SOLID)
  11. Title.pack(pady=10)
  12.  
  13. #========================================LABEL WIDGET=====================================
  14. lbl_display = Label(Title, text="Simple Inventory System", font=('arial', 45))
  15. lbl_display.pack()

Creating the Database Connection
Then after setting up the design we will now create the database function. To do that just simply copy the code below and paste it inside the IDLE text editor.

  1. #========================================METHODS==========================================
  2.  
  3. def Database():
  4. global conn, cursor
  5. conn = sqlite3.connect("pythontut.db")
  6. cursor = conn.cursor()
  7. cursor.execute("CREATE TABLE IF NOT EXISTS `admin` (admin_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT, password TEXT)")
  8. cursor.execute("CREATE TABLE IF NOT EXISTS `product` (product_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, product_name TEXT, product_qty TEXT, product_price TEXT)")
  9. cursor.execute("SELECT * FROM `admin` WHERE `username` = 'admin' AND `password` = 'admin'")
  10. if cursor.fetchone() is None:
  11. cursor.execute("INSERT INTO `admin` (username, password) VALUES('admin', 'admin')")
  12. conn.commit()

Creating The Main Function
This is the where main function located within the program. The program itself can add, view, and search all the products from the database.

  1. def Exit():
  2. result = tkMessageBox.askquestion('Simple Inventory System', 'Are you sure you want to exit?', icon="warning")
  3. if result == 'yes':
  4. root.destroy()
  5. exit()
  6.  
  7. def Exit2():
  8. result = tkMessageBox.askquestion('Simple Inventory System', 'Are you sure you want to exit?', icon="warning")
  9. if result == 'yes':
  10. Home.destroy()
  11. exit()
  12.  
  13. def ShowLoginForm():
  14. global loginform
  15. loginform = Toplevel()
  16. loginform.title("Simple Inventory System/Account Login")
  17. width = 600
  18. height = 500
  19. screen_width = root.winfo_screenwidth()
  20. screen_height = root.winfo_screenheight()
  21. x = (screen_width/2) - (width/2)
  22. y = (screen_height/2) - (height/2)
  23. loginform.resizable(0, 0)
  24. loginform.geometry("%dx%d+%d+%d" % (width, height, x, y))
  25. LoginForm()
  26.  
  27. def LoginForm():
  28. global lbl_result
  29. TopLoginForm = Frame(loginform, width=600, height=100, bd=1, relief=SOLID)
  30. TopLoginForm.pack(side=TOP, pady=20)
  31. lbl_text = Label(TopLoginForm, text="Administrator Login", font=('arial', 18), width=600)
  32. lbl_text.pack(fill=X)
  33. MidLoginForm = Frame(loginform, width=600)
  34. MidLoginForm.pack(side=TOP, pady=50)
  35. lbl_username = Label(MidLoginForm, text="Username:", font=('arial', 25), bd=18)
  36. lbl_username.grid(row=0)
  37. lbl_password = Label(MidLoginForm, text="Password:", font=('arial', 25), bd=18)
  38. lbl_password.grid(row=1)
  39. lbl_result = Label(MidLoginForm, text="", font=('arial', 18))
  40. lbl_result.grid(row=3, columnspan=2)
  41. username = Entry(MidLoginForm, textvariable=USERNAME, font=('arial', 25), width=15)
  42. username.grid(row=0, column=1)
  43. password = Entry(MidLoginForm, textvariable=PASSWORD, font=('arial', 25), width=15, show="*")
  44. password.grid(row=1, column=1)
  45. btn_login = Button(MidLoginForm, text="Login", font=('arial', 18), width=30, command=Login)
  46. btn_login.grid(row=2, columnspan=2, pady=20)
  47. btn_login.bind('<Return>', Login)
  48.  
  49. def Home():
  50. global Home
  51. Home = Tk()
  52. Home.title("Simple Inventory System/Home")
  53. width = 1024
  54. height = 720
  55. screen_width = Home.winfo_screenwidth()
  56. screen_height = Home.winfo_screenheight()
  57. x = (screen_width/2) - (width/2)
  58. y = (screen_height/2) - (height/2)
  59. Home.geometry("%dx%d+%d+%d" % (width, height, x, y))
  60. Home.resizable(0, 0)
  61. Title = Frame(Home, bd=1, relief=SOLID)
  62. Title.pack(pady=10)
  63. lbl_display = Label(Title, text="Simple Inventory System", font=('arial', 45))
  64. lbl_display.pack()
  65. menubar = Menu(Home)
  66. filemenu = Menu(menubar, tearoff=0)
  67. filemenu2 = Menu(menubar, tearoff=0)
  68. filemenu.add_command(label="Logout", command=Logout)
  69. filemenu.add_command(label="Exit", command=Exit2)
  70. filemenu2.add_command(label="Add new", command=ShowAddNew)
  71. filemenu2.add_command(label="View", command=ShowView)
  72. menubar.add_cascade(label="Account", menu=filemenu)
  73. menubar.add_cascade(label="Inventory", menu=filemenu2)
  74. Home.config(menu=menubar)
  75. Home.config(bg="#99ff99")
  76.  
  77. def ShowAddNew():
  78. global addnewform
  79. addnewform = Toplevel()
  80. addnewform.title("Simple Inventory System/Add new")
  81. width = 600
  82. height = 500
  83. screen_width = Home.winfo_screenwidth()
  84. screen_height = Home.winfo_screenheight()
  85. x = (screen_width/2) - (width/2)
  86. y = (screen_height/2) - (height/2)
  87. addnewform.geometry("%dx%d+%d+%d" % (width, height, x, y))
  88. addnewform.resizable(0, 0)
  89. AddNewForm()
  90.  
  91. def AddNewForm():
  92. TopAddNew = Frame(addnewform, width=600, height=100, bd=1, relief=SOLID)
  93. TopAddNew.pack(side=TOP, pady=20)
  94. lbl_text = Label(TopAddNew, text="Add New Product", font=('arial', 18), width=600)
  95. lbl_text.pack(fill=X)
  96. MidAddNew = Frame(addnewform, width=600)
  97. MidAddNew.pack(side=TOP, pady=50)
  98. lbl_productname = Label(MidAddNew, text="Product Name:", font=('arial', 25), bd=10)
  99. lbl_productname.grid(row=0, sticky=W)
  100. lbl_qty = Label(MidAddNew, text="Product Quantity:", font=('arial', 25), bd=10)
  101. lbl_qty.grid(row=1, sticky=W)
  102. lbl_price = Label(MidAddNew, text="Product Price:", font=('arial', 25), bd=10)
  103. lbl_price.grid(row=2, sticky=W)
  104. productname = Entry(MidAddNew, textvariable=PRODUCT_NAME, font=('arial', 25), width=15)
  105. productname.grid(row=0, column=1)
  106. productqty = Entry(MidAddNew, textvariable=PRODUCT_QTY, font=('arial', 25), width=15)
  107. productqty.grid(row=1, column=1)
  108. productprice = Entry(MidAddNew, textvariable=PRODUCT_PRICE, font=('arial', 25), width=15)
  109. productprice.grid(row=2, column=1)
  110. btn_add = Button(MidAddNew, text="Save", font=('arial', 18), width=30, bg="#009ACD", command=AddNew)
  111. btn_add.grid(row=3, columnspan=2, pady=20)
  112.  
  113. def AddNew():
  114. Database()
  115. cursor.execute("INSERT INTO `product` (product_name, product_qty, product_price) VALUES(?, ?, ?)", (str(PRODUCT_NAME.get()), int(PRODUCT_QTY.get()), int(PRODUCT_PRICE.get())))
  116. conn.commit()
  117. PRODUCT_NAME.set("")
  118. PRODUCT_PRICE.set("")
  119. PRODUCT_QTY.set("")
  120. cursor.close()
  121. conn.close()
  122.  
  123. def ViewForm():
  124. global tree
  125. TopViewForm = Frame(viewform, width=600, bd=1, relief=SOLID)
  126. TopViewForm.pack(side=TOP, fill=X)
  127. LeftViewForm = Frame(viewform, width=600)
  128. LeftViewForm.pack(side=LEFT, fill=Y)
  129. MidViewForm = Frame(viewform, width=600)
  130. MidViewForm.pack(side=RIGHT)
  131. lbl_text = Label(TopViewForm, text="View Products", font=('arial', 18), width=600)
  132. lbl_text.pack(fill=X)
  133. lbl_txtsearch = Label(LeftViewForm, text="Search", font=('arial', 15))
  134. lbl_txtsearch.pack(side=TOP, anchor=W)
  135. search = Entry(LeftViewForm, textvariable=SEARCH, font=('arial', 15), width=10)
  136. search.pack(side=TOP, padx=10, fill=X)
  137. btn_search = Button(LeftViewForm, text="Search", command=Search)
  138. btn_search.pack(side=TOP, padx=10, pady=10, fill=X)
  139. btn_reset = Button(LeftViewForm, text="Reset", command=Reset)
  140. btn_reset.pack(side=TOP, padx=10, pady=10, fill=X)
  141. btn_delete = Button(LeftViewForm, text="Delete", command=Delete)
  142. btn_delete.pack(side=TOP, padx=10, pady=10, fill=X)
  143. scrollbarx = Scrollbar(MidViewForm, orient=HORIZONTAL)
  144. scrollbary = Scrollbar(MidViewForm, orient=VERTICAL)
  145. tree = ttk.Treeview(MidViewForm, columns=("ProductID", "Product Name", "Product Qty", "Product Price"), selectmode="extended", height=100, yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
  146. scrollbary.config(command=tree.yview)
  147. scrollbary.pack(side=RIGHT, fill=Y)
  148. scrollbarx.config(command=tree.xview)
  149. scrollbarx.pack(side=BOTTOM, fill=X)
  150. tree.heading('ProductID', text="ProductID",anchor=W)
  151. tree.heading('Product Name', text="Product Name",anchor=W)
  152. tree.heading('Product Qty', text="Product Qty",anchor=W)
  153. tree.heading('Product Price', text="Product Price",anchor=W)
  154. tree.column('#0', stretch=NO, minwidth=0, width=0)
  155. tree.column('#1', stretch=NO, minwidth=0, width=0)
  156. tree.column('#2', stretch=NO, minwidth=0, width=200)
  157. tree.column('#3', stretch=NO, minwidth=0, width=120)
  158. tree.column('#4', stretch=NO, minwidth=0, width=120)
  159. tree.pack()
  160. DisplayData()
  161.  
  162. def DisplayData():
  163. Database()
  164. cursor.execute("SELECT * FROM `product`")
  165. fetch = cursor.fetchall()
  166. for data in fetch:
  167. tree.insert('', 'end', values=(data))
  168. cursor.close()
  169. conn.close()
  170.  
  171. def Search():
  172. if SEARCH.get() != "":
  173. tree.delete(*tree.get_children())
  174. Database()
  175. cursor.execute("SELECT * FROM `product` WHERE `product_name` LIKE ?", ('%'+str(SEARCH.get())+'%',))
  176. fetch = cursor.fetchall()
  177. for data in fetch:
  178. tree.insert('', 'end', values=(data))
  179. cursor.close()
  180. conn.close()
  181.  
  182. def Reset():
  183. tree.delete(*tree.get_children())
  184. DisplayData()
  185. SEARCH.set("")
  186.  
  187. def Delete():
  188. if not tree.selection():
  189. print("ERROR")
  190. else:
  191. result = tkMessageBox.askquestion('Simple Inventory System', 'Are you sure you want to delete this record?', icon="warning")
  192. if result == 'yes':
  193. curItem = tree.focus()
  194. contents =(tree.item(curItem))
  195. selecteditem = contents['values']
  196. tree.delete(curItem)
  197. Database()
  198. cursor.execute("DELETE FROM `product` WHERE `product_id` = %d" % selecteditem[0])
  199. conn.commit()
  200. cursor.close()
  201. conn.close()
  202.  
  203.  
  204. def ShowView():
  205. global viewform
  206. viewform = Toplevel()
  207. viewform.title("Simple Inventory System/View Product")
  208. width = 600
  209. height = 400
  210. screen_width = Home.winfo_screenwidth()
  211. screen_height = Home.winfo_screenheight()
  212. x = (screen_width/2) - (width/2)
  213. y = (screen_height/2) - (height/2)
  214. viewform.geometry("%dx%d+%d+%d" % (width, height, x, y))
  215. viewform.resizable(0, 0)
  216. ViewForm()
  217.  
  218. def Logout():
  219. result = tkMessageBox.askquestion('Simple Inventory System', 'Are you sure you want to logout?', icon="warning")
  220. if result == 'yes':
  221. admin_id = ""
  222. root.deiconify()
  223. Home.destroy()
  224.  
  225. def Login(event=None):
  226. global admin_id
  227. Database()
  228. if USERNAME.get == "" or PASSWORD.get() == "":
  229. lbl_result.config(text="Please complete the required field!", fg="red")
  230. else:
  231. cursor.execute("SELECT * FROM `admin` WHERE `username` = ? AND `password` = ?", (USERNAME.get(), PASSWORD.get()))
  232. if cursor.fetchone() is not None:
  233. cursor.execute("SELECT * FROM `admin` WHERE `username` = ? AND `password` = ?", (USERNAME.get(), PASSWORD.get()))
  234. data = cursor.fetchone()
  235. admin_id = data[0]
  236. USERNAME.set("")
  237. PASSWORD.set("")
  238. lbl_result.config(text="")
  239. ShowHome()
  240. else:
  241. lbl_result.config(text="Invalid username or password", fg="red")
  242. USERNAME.set("")
  243. PASSWORD.set("")
  244. cursor.close()
  245. conn.close()
  246.  
  247. def ShowHome():
  248. root.withdraw()
  249. Home()
  250. loginform.destroy()

Initializing the Application
After finishing the function save the application as 'index.py'. This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.

  1. #========================================INITIALIZATION===================================
  2. if __name__ == '__main__':
  3. root.mainloop()

There you have it we successfully created a Simple Inventory System Using Python / SQLite. I hope the this simple program help you for what you are looking for. For more updates and tutorials. Just kindly visit this site. Enjoy Coding!!!


Tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.