New Py
New Py
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 = {}
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