dontik
unknown
python
5 months ago
5.9 kB
6
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" ) # 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() # Update Product def update_product(): if not product_id.get(): messagebox.showwarning("Input Error", "Select a product to update!") return try: conn = connect_db() cursor = conn.cursor() query = """ UPDATE Products SET name=%s, quantity=%s, date_of_stock=%s, price=%s, product_type=%s WHERE id=%s """ cursor.execute(query, ( name_entry.get(), int(quantity_entry.get()), date_entry.get(), float(price_entry.get()), type_entry.get(), int(product_id.get()) )) conn.commit() messagebox.showinfo("Success", "Product updated successfully!") clear_entries() fetch_data() except Exception as e: messagebox.showerror("Error", str(e)) finally: conn.close() # Delete Product def delete_product(): if not product_id.get(): messagebox.showwarning("Input Error", "Select a product to delete!") return try: conn = connect_db() cursor = conn.cursor() query = "DELETE FROM Products WHERE id=%s" cursor.execute(query, (int(product_id.get()),)) conn.commit() messagebox.showinfo("Success", "Product deleted 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("") # On Row Select def on_row_select(event): selected = tree.selection() if selected: data = tree.item(selected[0], 'values') product_id.set(data[0]) name_entry.delete(0, tk.END) name_entry.insert(0, data[1]) quantity_entry.delete(0, tk.END) quantity_entry.insert(0, data[2]) date_entry.delete(0, tk.END) date_entry.insert(0, data[3]) price_entry.delete(0, tk.END) price_entry.insert(0, data[4]) type_entry.delete(0, tk.END) type_entry.insert(0, data[5]) # Tkinter GUI root = tk.Tk() root.title("Shop Management System") root.configure(bg="light gray") # Set background color # Variables product_id = tk.StringVar() # 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="Update Product", command=update_product, bg="light blue").grid(row=5, column=1, padx=10, pady=10) tk.Button(root, text="Delete Product", command=delete_product, bg="light blue").grid(row=5, column=2, padx=10, pady=10) tk.Button(root, text="Clear", command=clear_entries, bg="light blue").grid(row=5, column=3, 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) tree.bind("<ButtonRelease-1>", on_row_select) fetch_data() root.mainloop()
Editor is loading...
Leave a Comment