0% found this document useful (0 votes)
11 views3 pages

Python Shop Management System Code

The document contains a complete Python code for a Shop Management System that connects to a MySQL database. It includes functionalities for adding, removing, and billing stock items, along with a graphical user interface built using the customtkinter library. The system allows users to manage stock efficiently and provides feedback through message boxes for various operations.

Uploaded by

bingewatch0990
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Python Shop Management System Code

The document contains a complete Python code for a Shop Management System that connects to a MySQL database. It includes functionalities for adding, removing, and billing stock items, along with a graphical user interface built using the customtkinter library. The system allows users to manage stock efficiently and provides feedback through message boxes for various operations.

Uploaded by

bingewatch0990
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SHOP MANAGEMENT SYSTEM – PYTHON CODE

# SHOP MANAGEMENT SYSTEM – FULL WORKING PYTHON CODE (CLASS 12)

import [Link]
import customtkinter as ctk
from tkinter import *
from tkinter import ttk, messagebox

# -------------------------------------------------------
# DATABASE CONNECTION
# -------------------------------------------------------
def connect_db():
try:
mydb = [Link](
host="localhost",
user="root",
password="", # ENTER YOUR PASSWORD IF ANY
database="shopdb"
)
return mydb
except:
[Link]("Error", "MySQL Connection Failed")

# -------------------------------------------------------
# REFRESH TABLE
# -------------------------------------------------------
def refresh_stock():
for row in stock_table.get_children():
stock_table.delete(row)

db = connect_db()
cur = [Link]()
[Link]("SELECT * FROM stock")
rows = [Link]()
for r in rows:
stock_table.insert("", END, values=r)
[Link]()

# -------------------------------------------------------
# ADD STOCK
# -------------------------------------------------------
def add_stock():
code = entry_code.get()
name = entry_name.get()
price = entry_price.get()
qty = entry_qty.get()

if code == "" or name == "" or price == "" or qty == "":


[Link]("Warning", "All fields required")
return

try:
db = connect_db()
cur = [Link]()

[Link]("SELECT qty FROM stock WHERE item_code=%s", (code,))


existing = [Link]()

if existing:
new_qty = existing[0] + int(qty)
[Link]("UPDATE stock SET qty=%s WHERE item_code=%s", (new_qty, code))
else:
[Link](
"INSERT INTO stock (item_code, item_name, price, qty) VALUES (%s, %s, %s, %s)",
(code, name, price, qty)
)

[Link]()
[Link]()
[Link]("Success", "Stock Added")
refresh_stock()
except:
[Link]("Error", "Invalid Input")

# -------------------------------------------------------
# REMOVE STOCK
# -------------------------------------------------------
def remove_stock():
code = entry_code.get()
qty = entry_qty.get()

if code == "" or qty == "":


[Link]("Warning", "Enter Code and Quantity")
return

db = connect_db()
cur = [Link]()
[Link]("SELECT qty FROM stock WHERE item_code=%s", (code,))
existing = [Link]()

if not existing:
[Link]("Error", "Item not found!")
return

if existing[0] < int(qty):


[Link]("Error", "Not enough stock")
return

new_qty = existing[0] - int(qty)


[Link]("UPDATE stock SET qty=%s WHERE item_code=%s", (new_qty, code))

[Link]()
[Link]()
[Link]("Success", "Stock Removed")
refresh_stock()

# -------------------------------------------------------
# BILL / SALE
# -------------------------------------------------------
def billing():
code = entry_code.get()
qty = entry_qty.get()

if code == "" or qty == "":


[Link]("Warning", "Enter Code & Quantity")
return

db = connect_db()
cur = [Link]()

[Link]("SELECT item_name, price, qty FROM stock WHERE item_code=%s", (code,))


item = [Link]()

if not item:
[Link]("Error", "Item not found")
return

if item[2] < int(qty):


[Link]("Error", "Not enough stock")
return

new_qty = item[2] - int(qty)


[Link]("UPDATE stock SET qty=%s WHERE item_code=%s", (new_qty, code))

[Link](
"INSERT INTO sales (item_code, item_name, price, qty) VALUES (%s, %s, %s, %s)",
(code, item[0], item[1], qty)
)

[Link]()
[Link]()

[Link]("Success", "Billing Done")


refresh_stock()

# -------------------------------------------------------
# GUI DESIGN
# -------------------------------------------------------
root = [Link]()
[Link]("Shop Management System - Class 12")
[Link]("850x500")

title = [Link](root, text="SHOP MANAGEMENT SYSTEM", font=("Arial", 24))


[Link](pady=10)

frame = [Link](root)
[Link](pady=10)

# INPUT FIELDS
[Link](frame, text="Item Code").grid(row=0, column=0, padx=10)
entry_code = [Link](frame, width=180)
entry_code.grid(row=0, column=1)

[Link](frame, text="Item Name").grid(row=1, column=0, padx=10)


entry_name = [Link](frame, width=180)
entry_name.grid(row=1, column=1)

[Link](frame, text="Price").grid(row=2, column=0, padx=10)


entry_price = [Link](frame, width=180)
entry_price.grid(row=2, column=1)

[Link](frame, text="Quantity").grid(row=3, column=0, padx=10)


entry_qty = [Link](frame, width=180)
entry_qty.grid(row=3, column=1)

# BUTTONS
[Link](root, text="ADD STOCK", command=add_stock).pack(pady=5)
[Link](root, text="REMOVE STOCK", command=remove_stock).pack(pady=5)
[Link](root, text="BILL / SALE", command=billing).pack(pady=5)

# STOCK TABLE
table_frame = Frame(root)
table_frame.pack(pady=10)

scroll = Scrollbar(table_frame)
[Link](side=RIGHT, fill=Y)

stock_table = [Link](
table_frame,
columns=("code", "name", "price", "qty"),
show="headings",
yscrollcommand=[Link],
)

stock_table.pack()
[Link](command=stock_table.yview)

for col in ("code", "name", "price", "qty"):


stock_table.heading(col, text=[Link]())

refresh_stock()
[Link]()

You might also like