Inventory management is a crucial aspect of any business that deals with physical goods. Python provides various libraries to read and write files, making it an excellent choice for managing inventory. File handling is a powerful tool that allows us to manipulate files on a computer’s file system using programming languages like Python. In this article, we will explore how to implement an inventory management system in Python, Tkinter using file handling.
Inventory Management System Project using Tkinter
Add New Product to Inventory
This function adds a new inventory entry into a text file. It retrieves the item name and quantity from input fields, opens the file in append mode, and writes item_name, item_qty. Then it clears the input fields.
Python3
| defadd_inventory():    item_name =item_name_entry.get()    item_qty =int(item_qty_entry.get())    with open('inventory.txt', 'a') as file:        file.write(f'{item_name},{item_qty}\n')    item_name_entry.delete(0, tk.END)    item_qty_entry.delete(0, tk.END) | 
Updating the Inventory
This function update_inventory() updates the existing item_name and item_qty according to the given input updated values. It opens the file in write mode and reads the data from the current inventory. It loops over each line, looks for a match with the item_name, and updates the record if one is found.
Python3
| defupdate_inventory():    item_name =item_name_entry.get()    item_qty =int(item_qty_entry.get())    with open('inventory.txt', 'r') as file:        inventory_data =file.readlines()    with open('inventory.txt', 'w') as file:        forline ininventory_data:            name, qty =line.strip().split(',')            ifname ==item_name:                file.write(f'{name},{item_qty}\n')            else:                file.write(line)    item_name_entry.delete(0, tk.END)    item_qty_entry.delete(0, tk.END) | 
Search and Display the Current Inventory
This function pulls the item_name from the input field that needs to be searched. It iterates through each line of the inventory.txt file while it is open in read-only mode. Here we are using result_label to show the item_name and amount if the item name matches the search name.
Python3
| defsearch_inventory():    search_name =item_name_entry.get()    with open('inventory.txt', 'r') as file:        forline infile:            name, qty =line.strip().split(',')            ifname ==search_name:                result_label.config(text=f'{name} - {qty}')                return    result_label.config(text=f'{search_name} not found in inventory.')    item_name_entry.delete(0, tk.END) | 
Removing an Item from the Inventory
The remove_inventory() function removes a specific inventory entry from the inventory.txt file. The function retrieves the item name to be removed from the input field. It opens the inventory.txt file in read mode and reads all the inventory data into a list. The file is then opened again in write mode. It iterates over each line in the inventory data, checks if the item name matches the one to be removed, and skips writing that line.
Python3
| defremove_inventory():    remove_name =item_name_entry.get()    with open('inventory.txt', 'r') as file:        inventory_data =file.readlines()    with open('inventory.txt', 'w') as file:        forline ininventory_data:            name, qty =line.strip().split(',')            ifname !=remove_name:                file.write(line)    item_name_entry.delete(0, tk.END)    item_qty_entry.delete(0, tk.END) | 
Generate a Full Inventory List
The generate_inventory() function generates a full list of inventory entries from a text file. It reads the file and stores the inventory data in a list. The inventory data is then joined into a string with newlines. Finally, the string is displayed in the result_label to show the complete inventory list.
Python3
| defgenerate_inventory():    with open('inventory.txt', 'r') as file:        inventory_data =file.readlines()    inventory_text ='\n'.join(inventory_data)    result_label.config(text=inventory_text) | 
Complete CodeÂ
Python3
| importtkinter as tk  # Function to add a new inventory entrydefadd_inventory():    item_name =item_name_entry.get()    item_qty =int(item_qty_entry.get())    with open('inventory.txt', 'a') as file:        file.write(f'{item_name},{item_qty}\n')    item_name_entry.delete(0, tk.END)    item_qty_entry.delete(0, tk.END)  # Function to update an existing inventory entrydefupdate_inventory():    item_name =item_name_entry.get()    item_qty =int(item_qty_entry.get())    with open('inventory.txt', 'r') as file:        inventory_data =file.readlines()    with open('inventory.txt', 'w') as file:        forline ininventory_data:            name, qty =line.strip().split(',')            ifname ==item_name:                file.write(f'{name},{item_qty}\n')            else:                file.write(line)    item_name_entry.delete(0, tk.END)    item_qty_entry.delete(0, tk.END)  # Function to search and display an inventory entrydefsearch_inventory():    search_name =item_name_entry.get()    with open('inventory.txt', 'r') as file:        forline infile:            name, qty =line.strip().split(',')            ifname ==search_name:                result_label.config(text=f'{name} - {qty}')                return    result_label.config(text=f'{search_name} not found in inventory.')    item_name_entry.delete(0, tk.END)  # Function to remove an existing inventory entrydefremove_inventory():    remove_name =item_name_entry.get()    with open('inventory.txt', 'r') as file:        inventory_data =file.readlines()    with open('inventory.txt', 'w') as file:        forline ininventory_data:            name, qty =line.strip().split(',')            ifname !=remove_name:                file.write(line)    item_name_entry.delete(0, tk.END)    item_qty_entry.delete(0, tk.END)  # Function to generate a full list of inventorydefgenerate_inventory():    with open('inventory.txt', 'r') as file:        inventory_data =file.readlines()    inventory_text ='\n'.join(inventory_data)    result_label.config(text=inventory_text)  # create the main windowroot =tk.Tk()root.title("Inventory Management")  # input fieldsitem_name_label =tk.Label(root, text="Item Name:")item_name_label.grid(row=0, column=0, padx=5, pady=5)item_name_entry =tk.Entry(root)item_name_entry.grid(row=0, column=1, padx=5, pady=5)item_qty_label =tk.Label(root, text="Item Quantity:")item_qty_label.grid(row=1, column=0, padx=5, pady=5)item_qty_entry =tk.Entry(root)item_qty_entry.grid(row=1, column=1, padx=5, pady=5)  # creating the buttonsadd_button =tk.Button(root, text="Add Inventory",                        command=add_inventory)add_button.grid(row=2, column=0, padx=5, pady=5)update_button =tk.Button(root, text="Update Inventory",                           command=update_inventory)update_button.grid(row=2, column=1, padx=5, pady=5)search_button =tk.Button(root, text="Search Inventory",                           command=search_inventory)search_button.grid(row=3, column=0, padx=5, pady=5)remove_button =tk.Button(root, text="Remove Inventory",                           command=remove_inventory)remove_button.grid(row=3, column=1, padx=5, pady=5)generate_button =tk.Button(root, text="Generate Inventory",                             command=generate_inventory)generate_button.grid(row=4, column=0, padx=5, pady=5)  result_label =tk.Label(root, text="List")result_label.grid(row=5, column=0, padx=5, pady=5)  root.mainloop() | 
Output :

 
                                    







