Simple Inventory Management Project using Python and Tkinter

Inventory management is a crucial aspect of any business, and it involves the efficient handling and control of inventory items.

In this tutorial, we will discuss a simple inventory management system that was created using Python and Tkinter library.

The inventory management system is a graphical user interface (GUI) application that provides users with the ability to search for inventory items, add new items, edit existing items, and delete items.

The application is written in Python, a popular programming language that is easy to learn and use.

What is Tkinter?

The Tkinter module is used in this project to create the GUI components of the application. The Tkinter module is a standard Python library that provides a set of tools for creating graphical user interfaces. It is a powerful and flexible module that can be used to create simple and complex GUI applications.

Program Explanation:

Let’s start by creating a new Python file and importing the Tkinter library.

This is a simple Inventory Management program that is implemented in Python with the Tkinter GUI toolkit. The program is designed to allow users to manage an inventory of items. This provides a graphical user interface (GUI) that allows users to search, add, edit, and delete items in the inventory.

The program uses the following components of Tkinter to create the GUI:

  1. Tk() function: This function creates the main window for the application.
  2. Frame widget: This widget creates a container for other widgets.
  3. Label widget: This widget is used to create text labels in the GUI.
  4. Entry widget: This widget creates an input field that allows users to enter data.
  5. Button widget: This widget creates a button that triggers an action when clicked.
  6. Text widget: This widget creates a text field that allows users to display and edit text.
  7. Scrollbar widget: This widget creates a scrollbar that allows users to scroll through the text field.

Inventory Management Class:

The program uses the InventoryManagement class to create the main frame of the application. The class inherits from the Frame widget and provides the following methods:

  1. __init__(): This is the constructor method for the class. It initializes the main frame of the application and creates all the necessary widgets.
  2. addItem(): This method is used to add an item to the inventory. It retrieves the values from the input fields and appends them to a list of dictionaries.
  3. editItem(): This method is used to edit an existing item in the inventory. It retrieves the values from the input fields and updates the corresponding dictionary in the list.
  4. deleteItem(): This method is used to delete an existing item from the inventory. It retrieves the item number from the input field and removes the corresponding dictionary from the list.
  5. searchInventory(): This method is used to search for an item in the inventory. It retrieves the item number from the input field and searches the list of dictionaries for a match. If a match is found, the corresponding item is displayed in the text field.
  6. clearSearch(): This method is used to clear the search results from the text field.

The program creates a GUI with input fields for item number, item name, on hand, and price.

The user can add, edit, and delete items from the inventory and search for items by their item number.

The program also displays the total number of items in the inventory.

Complete Code:

''' Simple Inventory Management - Written by Daniel Miller for class CIT 144 - Python I
        Written with Python using Tkinter
'''

from tkinter import *

root = Tk()

class InventoryManagement(Frame):

    # Creates constructor for main frame of application

    def __init__(self):
        Frame.__init__(self)
        self.master.title('Inventory Management')
        self.grid()
        self.items = []
        root.geometry("650x450")

        self.itemCount = len(self.items)

        # Lines 23 - 36 are top of application, search feature labels/entry/buttons

        Label(self, text='Search (Item Number): ').grid(row=0,
                                                        column=1, padx=6, pady=20, sticky=E)

        self._box1 = IntVar()
        self._input = Entry(self, width=20, textvariable=self._box1)
        self._input.grid(row=0, column=2, padx=8, pady=20, sticky=W)

        self.btn1 = Button(self, text='Search',
                           command=self.searchInventory)
        self.btn1.grid(row=0, column=3, padx=8, pady=20, sticky=W)

        self.btn2 = Button(self, text='Reset', command=self.clearSearch)
        self.btn2.grid(row=0, column=4, padx=4, pady=20, sticky=W)

        # Lines 40 - 45 is the main text area for inventory display

        self.scroll = Scrollbar(self)
        self.scroll.grid(row=3, column=4)
        self.text = Text(self, width=60, height=10, wrap=WORD,
                         yscrollcommand=self.scroll.set)
        self.text.grid(row=3, column=0, columnspan=5, padx=20, pady=20)
        self.scroll.config(command=self.text.yview)

        Label(self, text="Item Count: " + str(self.itemCount)).grid(row=4, column=0, pady=5, sticky=N)

        # Lines 49 - 75 are labels/entry boxes for new/edit item entry

        Label(self, text='Item Number ').grid(row=6, column=0, padx=6,
                                              pady=6, sticky=W)

        self._box2 = StringVar()
        self._input1 = Entry(self, width=20, textvariable=self._box2)
        self._input1.grid(row=6, column=1, padx=8, pady=10, sticky=E)

        Label(self, text='Item Name ').grid(row=6, column=2, padx=6,
                                            pady=6, sticky=E)

        self._box3 = StringVar()
        self._input = Entry(self, width=20, textvariable=self._box3)
        self._input.grid(row=6, column=3, padx=8, pady=10, sticky=E)

        Label(self, text='On Hand ').grid(row=10, column=0, padx=6,
                                          pady=6, sticky=E)

        self._box4 = StringVar()
        self._input = Entry(self, width=20, textvariable=self._box4)
        self._input.grid(row=10, column=1, padx=8, pady=10, sticky=W)

        Label(self, text='Price ').grid(row=10, column=2, padx=6,
                                        pady=6, sticky=E)

        self._box5 = StringVar()
        self._input = Entry(self, width=20, textvariable=self._box5)
        self._input.grid(row=10, column=3, padx=8, pady=10)

        # Lines 79 - 88 are buttons for corresponding functions to add/edit/delete items from text area

        self.btn3 = Button(self, text='Add Item', command=self.addItem)
        self.btn3.grid(row=11, column=1, padx=5, pady=20, sticky=W)

        self.btn4 = Button(self, text='Edit Item',
                           command=self.editItem)
        self.btn4.grid(row=11, column=2, padx=5, pady=20, sticky=W)

        self.btn4 = Button(self, text='Delete Item',
                           command=self.deleteItem)
        self.btn4.grid(row=11, column=3, padx=5, pady=20, sticky=W)

        # Lines 91 - 98 inserts headers into text area and sets focus to Item Number entry box
        self.text.insert(END, 'Item Number' + '\t\t' + 'Item Name'
                         + '\t\t' + 'On Hand' + '\t\t' + 'Price'
                         + '\t\t')
        self.text.insert(END,
                         '------------------------------------------------------------'
                         )
        self.text.configure(state="disabled")
        self._input1.focus_set()

    ''' addItem() function inserts headers into text area, grabs values from entry boxes 
        and appends them to a list of dictionaries if entry boxes are not empty.  It then prints
        each item(dictionary) to the text area and clears the entry boxes. '''


    def addItem(self):

        self.text.configure(state="normal")
        self.text.delete(1.0, END)
        self.text.insert(END, 'Item Number' + '\t\t' + 'Item Name'
                         + '\t\t' + 'On Hand' + '\t\t' + 'Price'
                         + '\t\t')
        self.text.insert(END,
                         '------------------------------------------------------------'
                         )

        items = self.items

        iNum = self._box2.get()
        iName = self._box3.get()
        oHand = self._box4.get()
        iPrice = self._box5.get()
    
        if (iNum != '' and iName != '' and oHand != '' and iPrice != ''):
            record = {
                0: iNum,
                1: iName,
                2: oHand,
                3: iPrice,
            }
            items.append(record)

            for item in items:
                self.text.insert(END, item[0] + '\t\t' + item[1] + '\t\t'
                                 + item[2] + '\t\t' + item[3] + '\t\t')
        else:
            self.text.delete(1.0, END)
            self.text.insert(END, 'Error: One or more fields have been left blank.')

        self._box2.set('')
        self._box3.set('')
        self._box4.set('')
        self._box5.set('')
        self._input1.focus_set()

        self.text.configure(state="disabled")

        return

    ''' searchInventory() function inserts headers into text area, gets value of search box entry and compares to
        list of dictionaries.  If the search box value matches the item number key,
         it inserts the dictionaries values into the text area. '''


    def searchInventory(self):
        self.text.configure(state="normal")
        self.text.delete(1.0, END)
        self.text.insert(END, 'Item Number' + '\t\t' + 'Item Name'
                         + '\t\t' + 'On Hand' + '\t\t' + 'Price'
                         + '\t\t')
        self.text.insert(END,
                         '------------------------------------------------------------'
                         )

        searchVal = str(self._box1.get())

        for item in self.items:
            if item[0] == searchVal:
                self.text.insert(END, item[0] + '\t\t' + item[1]
                                 + '\t\t' + item[2] + '\t\t' + item[3]
                                 + '\t\t')

        self.text.configure(state="disabled")

    # Simple function attached to reset button to clear the search box

    def clearSearch(self):
        self._box1.set('')

    ''' editItem() function clears the entry boxes to prevent errors.  It then grabs the search box value and compares
        to the list of dictionaries.  If the dictionary's item number matches the value it inserts the value of the 
        dictionary into the entry boxes for editing. '''


    def editItem(self):
        self.text.configure(state="normal")
        self._box2.set('')
        self._box3.set('')
        self._box4.set('')
        self._box5.set('')

        items = self.items

        searchVal = str(self._box1.get())

        for item in items:
            if item[0] == searchVal:
                self.items.remove(item)
                self._box2.set(item[0])
                self._box3.set(item[1])
                self._box4.set(item[2])
                self._box5.set(item[3])

        self._box1.set('')
        self._input1.focus_set()

        self.text.configure(state="disabled")


    # Simple function to delete dictionary with item number that matches the search box value

    def deleteItem(self):
        self.text.configure(state="normal")
        self.text.delete(1.0, END)
        self.text.insert(END, 'Item Number' + '\t\t' + 'Item Name'
                         + '\t\t' + 'On Hand' + '\t\t' + 'Price'
                         + '\t\t')
        self.text.insert(END,
                         '------------------------------------------------------------'
                         )

        items = self.items

        searchVal = str(self._box1.get())

        for item in items:
            if item[0] == searchVal:
                self.items.remove(item)

        for item in items:
            self.text.insert(END, item[0] + '\t\t' + item[1] + '\t\t'
                             + item[2] + '\t\t' + item[3] + '\t\t')

        self._box1.set('')
        self.text.configure(state="disabled")

def main():
    InventoryManagement().mainloop()


main()
inventory management software

As you can see in this interface you can add, edit, and also delete items.

You can also search items in inventory management software using the search bar.

The reset button is also available to reset all data.

Conclusion:

So, this simple inventory management system is a great way to start with Python and Tkinter. It provides users with the ability to search for inventory items, add new items, edit existing items, and delete items. The system is easy to use and can be customized to meet the needs of any business. Python and Tkinter are powerful tools that can be used to create simple and complex GUI applications.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

4 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x