close
Itxperts

Inventory Management System using Python

In this project, we will build an Inventory Management System using Python. This system allows users to manage products, track stock, add new products, update existing product details, and generate reports on inventory status. We’ll use Tkinter for the graphical user interface (GUI) and SQLite to store the inventory data.

1. Project Setup

Modules Required:

  • tkinter: For creating the graphical user interface.
  • sqlite3: To store and manage the inventory records.

Install the necessary modules:

pip install tkinter

2. Project Features

  1. Add Product: Add new products to the inventory, including product name, quantity, and price.
  2. Update Product: Update existing products by changing their details like quantity and price.
  3. View Inventory: Display all products in the inventory with their details.
  4. Delete Product: Remove products from the inventory.
  5. Search Functionality: Search for products by name.
  6. Generate Reports: View a summary of products in stock.
  7. Database Integration: Use SQLite to store product details.

3. Database Design

We will create a single table in SQLite to store inventory data:

  1. Products Table:
    • id: (INTEGER PRIMARY KEY AUTOINCREMENT)
    • name: (TEXT)
    • quantity: (INTEGER)
    • price: (REAL)

4. Code Structure

We will divide the project into five main sections:

  1. Creating the GUI: The interface for the user to interact with the system.
  2. Handling Product Logic: Adding, updating, viewing, and deleting products.
  3. Database Connection: Creating the database and storing/retrieving product details.
  4. Search and Filter Functionality: Enabling users to search for specific products.
  5. Report Generation: Allowing users to generate inventory reports.

5. Creating the Database

Let’s first define the database connection and table creation:

import sqlite3

# Connect to SQLite database and create tables
def connect_db():
    conn = sqlite3.connect('inventory.db')
    c = conn.cursor()

    # Create Products Table
    c.execute('''CREATE TABLE IF NOT EXISTS products
                 (id INTEGER PRIMARY KEY AUTOINCREMENT,
                  name TEXT,
                  quantity INTEGER,
                  price REAL)''')

    conn.commit()
    conn.close()

connect_db()

6. Product Management Functions

A. Add New Product

def add_product(name, quantity, price):
    conn = sqlite3.connect('inventory.db')
    c = conn.cursor()

    # Insert the new product into the products table
    c.execute("INSERT INTO products (name, quantity, price) VALUES (?, ?, ?)", (name, quantity, price))

    conn.commit()
    conn.close()

B. View All Products

def view_products():
    conn = sqlite3.connect('inventory.db')
    c = conn.cursor()

    # Select all products from the products table
    c.execute("SELECT * FROM products")
    products = c.fetchall()

    conn.close()
    return products

C. Update Product Details

def update_product(product_id, new_name, new_quantity, new_price):
    conn = sqlite3.connect('inventory.db')
    c = conn.cursor()

    # Update the name, quantity, and price of the product
    c.execute("UPDATE products SET name = ?, quantity = ?, price = ? WHERE id = ?", (new_name, new_quantity, new_price, product_id))

    conn.commit()
    conn.close()

D. Delete a Product

def delete_product(product_id):
    conn = sqlite3.connect('inventory.db')
    c = conn.cursor()

    # Delete the product from the products table
    c.execute("DELETE FROM products WHERE id = ?", (product_id,))

    conn.commit()
    conn.close()

7. Building the GUI with Tkinter

We will now create a graphical interface using Tkinter for users to interact with the system. Users will be able to add, view, update, and delete products from the inventory.

A. Main Window

from tkinter import *
from tkinter import messagebox

# Function to add a new product to the inventory
def save_product():
    name = name_entry.get()
    quantity = quantity_entry.get()
    price = price_entry.get()

    if name and quantity and price:
        add_product(name, int(quantity), float(price))
        messagebox.showinfo("Success", "Product added to inventory!")
        name_entry.delete(0, END)
        quantity_entry.delete(0, END)
        price_entry.delete(0, END)
    else:
        messagebox.showwarning("Error", "Please fill in all fields!")

# Function to display all products in the inventory
def display_products():
    products_window = Toplevel(root)
    products_window.title("View Inventory")

    products = view_products()

    for product in products:
        Label(products_window, text=f"ID: {product[0]}, Name: {product[1]}, Quantity: {product[2]}, Price: ${product[3]:.2f}").pack(pady=5)

# Main GUI window
root = Tk()
root.title("Inventory Management System")
root.geometry("400x400")

# Labels and text fields for product name, quantity, and price
Label(root, text="Product Name:", font=("Helvetica", 12)).pack(pady=10)
name_entry = Entry(root, width=40)
name_entry.pack(pady=5)

Label(root, text="Quantity:", font=("Helvetica", 12)).pack(pady=10)
quantity_entry = Entry(root, width=40)
quantity_entry.pack(pady=5)

Label(root, text="Price ($):", font=("Helvetica", 12)).pack(pady=10)
price_entry = Entry(root, width=40)
price_entry.pack(pady=5)

# Buttons to save the product and view all products
Button(root, text="Add Product", command=save_product).pack(pady=10)
Button(root, text="View Inventory", command=display_products).pack(pady=5)

# Run the GUI loop
root.mainloop()

8. Explanation of Code

A. Saving a New Product

  • The save_product() function collects the product name, quantity, and price from the user input, validates the data, and stores the product using the add_product() function.

B. Displaying All Products

  • The display_products() function opens a new window that lists all the products stored in the inventory. It retrieves the data from the database using the view_products() function.

9. Enhancements and Additional Features

Here are some ideas to extend the functionality of the Inventory Management System:

  1. Search by Product Name: Allow users to search for products based on their names.
  2. Stock Alerts: Send notifications when stock levels for certain products are low.
  3. Product Categories: Categorize products (e.g., electronics, clothing) for better management.
  4. Sorting: Sort products by name, quantity, or price.
  5. Report Generation: Create PDF or Excel reports for inventory summaries.

10. Conclusion

The Inventory Management System allows users to efficiently manage products in their inventory. It covers essential programming concepts like GUI development with Tkinter, database management with SQLite, and CRUD operations (Create, Read, Update, Delete). This project can be further enhanced with features like product search, stock alerts, and detailed report generation.

Would you like to add any additional features or modifications to this project?