ISP32 DEMO
import tkinter as tk
from tkinter import messagebox
import mysql.connector
# ---------------- DB ----------------
conn = mysql.connector.connect(
host="localhost",
user="root",
password="12345",
database="shop"
)
cur = conn.cursor(dictionary=True)
# ---------------- SQL ----------------
def login_user(login, password):
cur.execute(
"SELECT * FROM users WHERE login=%s AND password=%s",
(login, password)
)
return cur.fetchone()
def get_products():
cur.execute("SELECT * FROM products")
return cur.fetchall()
def search_products(text):
cur.execute("""
SELECT * FROM products
WHERE name LIKE %s OR category LIKE %s OR supplier LIKE %s
""", (f"%{text}%", f"%{text}%", f"%{text}%"))
return cur.fetchall()
def delete_product(pid):
cur.execute("DELETE FROM products WHERE id=%s", (pid,))
conn.commit()
def add_product(name, category, supplier, price, stock, discount):
cur.execute("""
INSERT INTO products (name, category, supplier, price, stock_quantity, discount)
VALUES (%s,%s,%s,%s,%s,%s)
""", (name, category, supplier, price, stock, discount))
conn.commit()
# ---------------- PRODUCTS WINDOW ----------------
def open_products(user):
win = tk.Tk()
win.title("Products System")
win.geometry("900x500")
# HEADER
tk.Label(
win,
text=f"{user['fio']} | {user['role']}",
font=("Arial", 12, "bold")
).pack()
# SEARCH
top_frame = tk.Frame(win)
top_frame.pack(fill="x")
search_entry = tk.Entry(top_frame)
search_entry.pack(side="left", padx=5)
# MAIN TABLE FRAME
frame = tk.Frame(win)
frame.pack(fill="both", expand=True)
# ---------------- LOAD ----------------
def load(data):
for w in frame.winfo_children():
w.destroy()
# header row
header = tk.Frame(frame, bg="lightgray")
header.pack(fill="x")
tk.Label(header, text="ID", width=5, bg="lightgray").pack(side="left")
tk.Label(header, text="Name", width=20, bg="lightgray").pack(side="left")
tk.Label(header, text="Price", width=10, bg="lightgray").pack(side="left")
tk.Label(header, text="Stock", width=10, bg="lightgray").pack(side="left")
tk.Label(header, text="Discount", width=10, bg="lightgray").pack(side="left")
# data rows
for p in data:
color = "white"
if p["stock_quantity"] == 0:
color = "lightblue"
elif p["discount"] > 15:
color = "#2E8B57"
row = tk.Frame(frame, bg=color)
row.pack(fill="x")
tk.Label(row, text=p["id"], width=5, bg=color).pack(side="left")
tk.Label(row, text=p["name"], width=20, bg=color).pack(side="left")
tk.Label(row, text=p["price"], width=10, bg=color).pack(side="left")
tk.Label(row, text=p["stock_quantity"], width=10, bg=color).pack(side="left")
tk.Label(row, text=p["discount"], width=10, bg=color).pack(side="left")
# ADMIN BUTTONS
if user["role"] == "admin":
tk.Button(
row,
text="Delete",
command=lambda i=p["id"]: delete_and_reload(i)
).pack(side="right")
# ---------------- ACTIONS ----------------
def delete_and_reload(pid):
delete_product(pid)
load(get_products())
def do_search():
text = search_entry.get()
load(search_products(text))
def reset():
load(get_products())
# BUTTONS
tk.Button(top_frame, text="Search", command=do_search).pack(side="left")
tk.Button(top_frame, text="Reset", command=reset).pack(side="left")
# ADD BUTTON (ONLY ADMIN)
def add_window():
w = tk.Toplevel()
w.title("Add Product")
e1 = tk.Entry(w); e1.pack(); e1.insert(0,"name")
e2 = tk.Entry(w); e2.pack(); e2.insert(0,"category")
e3 = tk.Entry(w); e3.pack(); e3.insert(0,"supplier")
e4 = tk.Entry(w); e4.pack(); e4.insert(0,"price")
e5 = tk.Entry(w); e5.pack(); e5.insert(0,"stock")
e6 = tk.Entry(w); e6.pack(); e6.insert(0,"discount")
def save():
add_product(
e1.get(),
e2.get(),
e3.get(),
float(e4.get()),
int(e5.get()),
int(e6.get())
)
w.destroy()
load(get_products())
tk.Button(w, text="Save", command=save).pack()
if user["role"] == "admin":
tk.Button(win, text="Add product", command=add_window).pack()
# INIT LOAD
load(get_products())
win.mainloop()
# ---------------- LOGIN ----------------
root = tk.Tk()
root.title("Login")
root.geometry("300x200")
tk.Label(root, text="Login").pack()
login_e = tk.Entry(root)
login_e.pack()
tk.Label(root, text="Password").pack()
pass_e = tk.Entry(root, show="*")
pass_e.pack()
def login():
user = login_user(login_e.get(), pass_e.get())
if user:
root.destroy()
open_products(user)
else:
messagebox.showerror("Error", "Wrong login")
tk.Button(root, text="Login", command=login).pack()
root.mainloop()