Giới thiệu sản phẩm
import tkinter as tk
from tkinter import messagebox
# Danh sách sản phẩm (id, tên sản phẩm, giá)
products = [
{'id': 1, 'name': 'Sản phẩm A', 'price': 100000},
{'id': 2, 'name': 'Sản phẩm B', 'price': 150000},
{'id': 3, 'name': 'Sản phẩm C', 'price': 200000}
]
# Giỏ hàng để lưu các sản phẩm đã chọn
cart = []
# Hàm thêm sản phẩm vào giỏ hàng
def add_to_cart(product):
cart.append(product)
update_cart()
# Hàm cập nhật giỏ hàng và tính tổng tiền
def update_cart():
cart_listbox.delete(0, tk.END) # Xóa danh sách giỏ hàng hiện tại
total_price = 0
for product in cart:
cart_listbox.insert(tk.END, f"{product['name']} - {product['price']} VND")
total_price += product['price']
total_label.config(text=f"Tổng tiền: {total_price} VND")
# Hàm thanh toán giỏ hàng
def checkout():
if not cart:
messagebox.showwarning("Giỏ hàng trống", "Giỏ hàng của bạn đang trống!")
else:
total = sum([product['price'] for product in cart])
messagebox.showinfo("Thanh toán thành công", f"Bạn đã thanh toán thành công {total} VND!")
# Tạo cửa sổ ứng dụng
root = tk.Tk()
root.title("App Bán Hàng Sen Đỏ")
# Khung sản phẩm
product_frame = tk.LabelFrame(root, text="Danh sách sản phẩm", padx=10, pady=10)
product_frame.pack(padx=20, pady=10, fill="both", expand=True)
# Hiển thị sản phẩm
for product in products:
button = tk.Button(product_frame, text=f"{product['name']} - {product['price']} VND",
command=lambda p=product: add_to_cart(p))
button.pack(fill="x", pady=5)
# Khung giỏ hàng
cart_frame = tk.LabelFrame(root, text="Giỏ hàng", padx=10, pady=10)
cart_frame.pack(padx=20, pady=10, fill="both", expand=True)
# Listbox hiển thị các sản phẩm trong giỏ hàng
cart_listbox = tk.Listbox(cart_frame, height=6)
cart_listbox.pack(fill="both", expand=True)
# Tổng tiền giỏ hàng
total_label = tk.Label(cart_frame, text="Tổng tiền: 0 VND", font=('Arial', 12))
total_label.pack(pady=10)
# Nút thanh toán
checkout_button = tk.Button(root, text="Thanh toán", command=checkout)
checkout_button.pack(pady=20)
# Khởi động ứng dụng
root.mainloop()
from tkinter import messagebox
# Danh sách sản phẩm (id, tên sản phẩm, giá)
products = [
{'id': 1, 'name': 'Sản phẩm A', 'price': 100000},
{'id': 2, 'name': 'Sản phẩm B', 'price': 150000},
{'id': 3, 'name': 'Sản phẩm C', 'price': 200000}
]
# Giỏ hàng để lưu các sản phẩm đã chọn
cart = []
# Hàm thêm sản phẩm vào giỏ hàng
def add_to_cart(product):
cart.append(product)
update_cart()
# Hàm cập nhật giỏ hàng và tính tổng tiền
def update_cart():
cart_listbox.delete(0, tk.END) # Xóa danh sách giỏ hàng hiện tại
total_price = 0
for product in cart:
cart_listbox.insert(tk.END, f"{product['name']} - {product['price']} VND")
total_price += product['price']
total_label.config(text=f"Tổng tiền: {total_price} VND")
# Hàm thanh toán giỏ hàng
def checkout():
if not cart:
messagebox.showwarning("Giỏ hàng trống", "Giỏ hàng của bạn đang trống!")
else:
total = sum([product['price'] for product in cart])
messagebox.showinfo("Thanh toán thành công", f"Bạn đã thanh toán thành công {total} VND!")
# Tạo cửa sổ ứng dụng
root = tk.Tk()
root.title("App Bán Hàng Sen Đỏ")
# Khung sản phẩm
product_frame = tk.LabelFrame(root, text="Danh sách sản phẩm", padx=10, pady=10)
product_frame.pack(padx=20, pady=10, fill="both", expand=True)
# Hiển thị sản phẩm
for product in products:
button = tk.Button(product_frame, text=f"{product['name']} - {product['price']} VND",
command=lambda p=product: add_to_cart(p))
button.pack(fill="x", pady=5)
# Khung giỏ hàng
cart_frame = tk.LabelFrame(root, text="Giỏ hàng", padx=10, pady=10)
cart_frame.pack(padx=20, pady=10, fill="both", expand=True)
# Listbox hiển thị các sản phẩm trong giỏ hàng
cart_listbox = tk.Listbox(cart_frame, height=6)
cart_listbox.pack(fill="both", expand=True)
# Tổng tiền giỏ hàng
total_label = tk.Label(cart_frame, text="Tổng tiền: 0 VND", font=('Arial', 12))
total_label.pack(pady=10)
# Nút thanh toán
checkout_button = tk.Button(root, text="Thanh toán", command=checkout)
checkout_button.pack(pady=20)
# Khởi động ứng dụng
root.mainloop()
Hình ảnh sản phẩm
Sản phẩm cùng tác giả
Sản phẩm liên quan
Không có sản phẩm nào
Đăng nhập để tham gia bình luận