In this project, we will create an E-commerce Management System using Python, allowing basic management of products, customers, and orders. We’ll use Tkinter for the GUI and SQLite to store data.
1. Project Setup
Modules Required:
- tkinter: For creating the graphical user interface.
- sqlite3: For managing the product, customer, and order data in a local database.
Install the necessary modules:
pip install tkinter
2. Project Features
- Manage Products: Add, update, delete, and view products.
- Manage Customers: Add, update, delete, and view customer details.
- Manage Orders: Place, view, and manage customer orders.
- Database Integration: Use SQLite to store and manage product, customer, and order information.
3. Database Design
We will create three tables in SQLite:
- Products Table:
- id: (INTEGER PRIMARY KEY AUTOINCREMENT)
- name: (TEXT)
- price: (REAL)
- quantity: (INTEGER)
- Customers Table:
- id: (INTEGER PRIMARY KEY AUTOINCREMENT)
- name: (TEXT)
- email: (TEXT)
- phone: (TEXT)
- Orders Table:
- id: (INTEGER PRIMARY KEY AUTOINCREMENT)
- customer_id: (INTEGER, Foreign Key referencing Customers Table)
- product_id: (INTEGER, Foreign Key referencing Products Table)
- quantity: (INTEGER)
- total_price: (REAL)
4. Code Structure
We will divide the project into three main sections:
- Product Management: Add, view, update, and delete products.
- Customer Management: Add, view, update, and delete customers.
- Order Management: Place new orders and view order history.
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('ecommerce.db')
c = conn.cursor()
# Create Products Table
c.execute('''CREATE TABLE IF NOT EXISTS products
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
price REAL,
quantity INTEGER)''')
# Create Customers Table
c.execute('''CREATE TABLE IF NOT EXISTS customers
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
email TEXT,
phone TEXT)''')
# Create Orders Table
c.execute('''CREATE TABLE IF NOT EXISTS orders
(id INTEGER PRIMARY KEY AUTOINCREMENT,
customer_id INTEGER,
product_id INTEGER,
quantity INTEGER,
total_price REAL,
FOREIGN KEY (customer_id) REFERENCES customers(id),
FOREIGN KEY (product_id) REFERENCES products(id))''')
conn.commit()
conn.close()
connect_db()
6. Product Management
A. Adding a New Product
def add_product(name, price, quantity):
conn = sqlite3.connect('ecommerce.db')
c = conn.cursor()
c.execute("INSERT INTO products (name, price, quantity) VALUES (?, ?, ?)", (name, price, quantity))
conn.commit()
conn.close()
# Example usage
add_product('Laptop', 75000, 10)
B. Viewing All Products
def view_products():
conn = sqlite3.connect('ecommerce.db')
c = conn.cursor()
c.execute("SELECT * FROM products")
products = c.fetchall()
conn.close()
return products
# Example usage
for product in view_products():
print(product)
C. Updating a Product
def update_product(product_id, name, price, quantity):
conn = sqlite3.connect('ecommerce.db')
c = conn.cursor()
c.execute("UPDATE products SET name=?, price=?, quantity=? WHERE id=?", (name, price, quantity, product_id))
conn.commit()
conn.close()
D. Deleting a Product
def delete_product(product_id):
conn = sqlite3.connect('ecommerce.db')
c = conn.cursor()
c.execute("DELETE FROM products WHERE id=?", (product_id,))
conn.commit()
conn.close()
7. Customer Management
A. Adding a New Customer
def add_customer(name, email, phone):
conn = sqlite3.connect('ecommerce.db')
c = conn.cursor()
c.execute("INSERT INTO customers (name, email, phone) VALUES (?, ?, ?)", (name, email, phone))
conn.commit()
conn.close()
# Example usage
add_customer('John Doe', 'john@example.com', '1234567890')
B. Viewing All Customers
def view_customers():
conn = sqlite3.connect('ecommerce.db')
c = conn.cursor()
c.execute("SELECT * FROM customers")
customers = c.fetchall()
conn.close()
return customers
# Example usage
for customer in view_customers():
print(customer)
C. Updating a Customer
def update_customer(customer_id, name, email, phone):
conn = sqlite3.connect('ecommerce.db')
c = conn.cursor()
c.execute("UPDATE customers SET name=?, email=?, phone=? WHERE id=?", (name, email, phone, customer_id))
conn.commit()
conn.close()
D. Deleting a Customer
def delete_customer(customer_id):
conn = sqlite3.connect('ecommerce.db')
c = conn.cursor()
c.execute("DELETE FROM customers WHERE id=?", (customer_id,))
conn.commit()
conn.close()
8. Order Management
A. Placing an Order
To place an order, we need the customer ID, product ID, and quantity. The total price will be calculated based on the product price and quantity.
def place_order(customer_id, product_id, quantity):
conn = sqlite3.connect('ecommerce.db')
c = conn.cursor()
# Get product price
c.execute("SELECT price FROM products WHERE id=?", (product_id,))
product = c.fetchone()
if product:
total_price = product[0] * quantity
c.execute("INSERT INTO orders (customer_id, product_id, quantity, total_price) VALUES (?, ?, ?, ?)",
(customer_id, product_id, quantity, total_price))
conn.commit()
conn.close()
# Example usage
place_order(1, 1, 2)
B. Viewing Orders
def view_orders():
conn = sqlite3.connect('ecommerce.db')
c = conn.cursor()
c.execute('''SELECT orders.id, customers.name, products.name, orders.quantity, orders.total_price
FROM orders
JOIN customers ON orders.customer_id = customers.id
JOIN products ON orders.product_id = products.id''')
orders = c.fetchall()
conn.close()
return orders
# Example usage
for order in view_orders():
print(order)
9. Building the GUI with Tkinter
A. Main Menu GUI
from tkinter import *
def open_product_window():
pass # Define product window functions
def open_customer_window():
pass # Define customer window functions
def open_order_window():
pass # Define order window functions
root = Tk()
root.title("E-commerce Management System")
root.geometry("400x400")
Button(root, text="Manage Products", command=open_product_window).pack(pady=20)
Button(root, text="Manage Customers", command=open_customer_window).pack(pady=20)
Button(root, text="Manage Orders", command=open_order_window).pack(pady=20)
root.mainloop()
You can create separate windows for managing products, customers, and orders by defining the open_product_window
, open_customer_window
, and open_order_window
functions.
10. Conclusion
The E-commerce Management System allows users to manage products, customers, and orders using Python and SQLite. It can be extended with additional features, including inventory management, sales reporting, and customer feedback.
Would you like to add any specific functionality or make adjustments to this project?