0% found this document useful (0 votes)
7 views

New Py

Uploaded by

dovinduchanthula
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

New Py

Uploaded by

dovinduchanthula
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

# gui_cashier_system.

py

import tkinter as tk
from tkinter import messagebox

class CashierApp:
def __init__(self, root):
self.root = root
self.root.title("Cashier System")

self.cart = {}

# Main interface elements


self.item_name_label = tk.Label(root, text="Item Name:")
self.item_name_label.grid(row=0, column=0, padx=5, pady=5)
self.item_name_entry = tk.Entry(root)
self.item_name_entry.grid(row=0, column=1, padx=5, pady=5)

self.item_price_label = tk.Label(root, text="Item Price:")


self.item_price_label.grid(row=1, column=0, padx=5, pady=5)
self.item_price_entry = tk.Entry(root)
self.item_price_entry.grid(row=1, column=1, padx=5, pady=5)

self.item_quantity_label = tk.Label(root, text="Quantity:")


self.item_quantity_label.grid(row=2, column=0, padx=5, pady=5)
self.item_quantity_entry = tk.Entry(root)
self.item_quantity_entry.grid(row=2, column=1, padx=5, pady=5)

self.add_button = tk.Button(root, text="Add to Cart",


command=self.add_item_to_cart)
self.add_button.grid(row=3, column=0, columnspan=2, pady=10)

self.cart_button = tk.Button(root, text="View Cart",


command=self.view_cart)
self.cart_button.grid(row=4, column=0, columnspan=2, pady=10)

self.checkout_button = tk.Button(root, text="Checkout",


command=self.checkout)
self.checkout_button.grid(row=5, column=0, columnspan=2, pady=10)

self.exit_button = tk.Button(root, text="Exit", command=root.quit)


self.exit_button.grid(row=6, column=0, columnspan=2, pady=10)

def add_item_to_cart(self):
"""
Adds an item to the cart.
"""
item_name = self.item_name_entry.get().strip()
try:
item_price = float(self.item_price_entry.get())
item_quantity = int(self.item_quantity_entry.get())
except ValueError:
messagebox.showerror("Invalid Input", "Price must be a number and
quantity must be an integer.")
return

if item_name:
if item_name in self.cart:
self.cart[item_name]['quantity'] += item_quantity
else:
self.cart[item_name] = {'price': item_price, 'quantity':
item_quantity}
messagebox.showinfo("Success", f"Added {item_quantity} x {item_name} to
the cart.")
else:
messagebox.showerror("Invalid Input", "Item name cannot be empty.")

self.item_name_entry.delete(0, tk.END)
self.item_price_entry.delete(0, tk.END)
self.item_quantity_entry.delete(0, tk.END)

def view_cart(self):
"""
Displays the current items in the cart.
"""
if not self.cart:
messagebox.showinfo("Cart", "Your cart is empty.")
return

cart_details = "\n".join(
f"{item}: ${details['price']} x {details['quantity']} = $
{details['price'] * details['quantity']:.2f}"
for item, details in self.cart.items()
)
total = self.calculate_total()
cart_details += f"\n\nTotal: ${total:.2f}"
messagebox.showinfo("Cart Items", cart_details)

def calculate_total(self):
"""
Calculates the total price of the items in the cart.
"""
return sum(details['price'] * details['quantity'] for details in
self.cart.values())

def checkout(self):
"""
Handles the checkout process.
"""
if not self.cart:
messagebox.showinfo("Checkout", "Your cart is empty. Nothing to
checkout.")
return

total = self.calculate_total()
amount_paid = tk.simpledialog.askfloat("Checkout", f"Total: ${total:.2f}\
nEnter the amount paid:")

if amount_paid is None:
return # User canceled input

if amount_paid < total:


messagebox.showerror("Insufficient Payment", f"Insufficient payment.
You are short by ${total - amount_paid:.2f}.")
else:
change = amount_paid - total
self.print_receipt(total, amount_paid, change)
self.cart.clear()
def print_receipt(self, total, amount_paid, change):
"""
Prints a receipt for the transaction.
"""
receipt = "\n".join(
f"{item}: ${details['price']} x {details['quantity']} = $
{details['price'] * details['quantity']:.2f}"
for item, details in self.cart.items()
)
receipt += f"\n\nTotal: ${total:.2f}"
receipt += f"\nAmount Paid: ${amount_paid:.2f}"
receipt += f"\nChange: ${change:.2f}"
messagebox.showinfo("Receipt", receipt)

# Run the application


if __name__ == "__main__":
root = tk.Tk()
app = CashierApp(root)
root.mainloop()

You might also like