Untitled

 avatar
unknown
python
5 months ago
4.6 kB
4
Indexable
import tkinter as tk
from tkinter import messagebox, ttk
import mysql.connector

# Database connection
def connect_db():
    return mysql.connector.connect(
        host="localhost",
        user="root",  # Replace with your MySQL username
        password="1234",  # Replace with your MySQL password
        database="ShopDB"
    )

# Main Application Window
def open_main_window():
    # Close the starting window
    start_window.destroy()
    
    # Create the main interface window
    root = tk.Toplevel()
    root.title("Shop Management System")
    root.configure(bg="light gray")  # Set background color

    # Variables
    product_id = tk.StringVar()

    # Add product
    def add_product():
        name = name_entry.get()
        quantity = quantity_entry.get()
        date = date_entry.get()
        price = price_entry.get()
        product_type = type_entry.get()
        
        if not (name and quantity and date and price and product_type):
            messagebox.showwarning("Input Error", "All fields are required!")
            return

        try:
            conn = connect_db()
            cursor = conn.cursor()
            query = "INSERT INTO Products (name, quantity, date_of_stock, price, product_type) VALUES (%s, %s, %s, %s, %s)"
            cursor.execute(query, (name, int(quantity), date, float(price), product_type))
            conn.commit()
            messagebox.showinfo("Success", "Product added successfully!")
            clear_entries()
            fetch_data()
        except Exception as e:
            messagebox.showerror("Error", str(e))
        finally:
            conn.close()

    # Fetch Data
    def fetch_data():
        try:
            conn = connect_db()
            cursor = conn.cursor()
            query = "SELECT * FROM Products"
            cursor.execute(query)
            rows = cursor.fetchall()
            for row in tree.get_children():
                tree.delete(row)
            for row in rows:
                tree.insert("", tk.END, values=row)
        except Exception as e:
            messagebox.showerror("Error", str(e))
        finally:
            conn.close()

    # Clear Entries
    def clear_entries():
        name_entry.delete(0, tk.END)
        quantity_entry.delete(0, tk.END)
        date_entry.delete(0, tk.END)
        price_entry.delete(0, tk.END)
        type_entry.delete(0, tk.END)
        product_id.set("")

    # Labels and Entry Fields
    tk.Label(root, text="Product Name", bg="light blue").grid(row=0, column=0, padx=10, pady=5)
    name_entry = tk.Entry(root)
    name_entry.grid(row=0, column=1, padx=10, pady=5)

    tk.Label(root, text="Quantity", bg="light blue").grid(row=1, column=0, padx=10, pady=5)
    quantity_entry = tk.Entry(root)
    quantity_entry.grid(row=1, column=1, padx=10, pady=5)

    tk.Label(root, text="Date of Stock (YYYY-MM-DD)", bg="light blue").grid(row=2, column=0, padx=10, pady=5)
    date_entry = tk.Entry(root)
    date_entry.grid(row=2, column=1, padx=10, pady=5)

    tk.Label(root, text="Price", bg="light blue").grid(row=3, column=0, padx=10, pady=5)
    price_entry = tk.Entry(root)
    price_entry.grid(row=3, column=1, padx=10, pady=5)

    tk.Label(root, text="Product Type", bg="light blue").grid(row=4, column=0, padx=10, pady=5)
    type_entry = tk.Entry(root)
    type_entry.grid(row=4, column=1, padx=10, pady=5)

    # Buttons
    tk.Button(root, text="Add Product", command=add_product, bg="light blue").grid(row=5, column=0, padx=10, pady=10)
    tk.Button(root, text="Clear", command=clear_entries, bg="light blue").grid(row=5, column=1, padx=10, pady=10)

    # Treeview for Display
    columns = ("ID", "Name", "Quantity", "Date of Stock", "Price", "Product Type")
    tree = ttk.Treeview(root, columns=columns, show="headings")
    for col in columns:
        tree.heading(col, text=col)
    tree.grid(row=6, column=0, columnspan=4, padx=10, pady=10)

    fetch_data()

# Starting Window
start_window = tk.Tk()
start_window.title("Welcome to Shop Management System")
start_window.geometry("400x300")
start_window.configure(bg="light gray")

welcome_label = tk.Label(
    start_window, text="Welcome to Shop Management System", 
    font=("Arial", 16, "bold"), bg="light gray", fg="dark blue"
)
welcome_label.pack(pady=50)

start_button = tk.Button(
    start_window, text="Enter System", font=("Arial", 14), 
    bg="dark blue", fg="white", command=open_main_window
)
start_button.pack(pady=20)

start_window.mainloop()
Editor is loading...
Leave a Comment