|
|
|
|
import tkinter as tk
|
|
|
|
|
from tkinter import messagebox
|
|
|
|
|
from tkinter import simpledialog
|
|
|
|
|
|
|
|
|
|
# 假设有一个UserDB类用于数据库操作,这里简化处理
|
|
|
|
|
|
|
|
|
|
class UserDB:
|
|
|
|
|
@staticmethod
|
|
|
|
|
def user_exists(username):
|
|
|
|
|
# 实际情况下,这里应该查询数据库
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def user_login(username, password):
|
|
|
|
|
# 实际情况下,这里应该从数据库查询用户信息并验证密码
|
|
|
|
|
# 返回True表示验证成功,False表示失败
|
|
|
|
|
return True if username == "test" and password == "test" else False # 示例验证逻辑,实际应用需替换
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 假设的商品列表
|
|
|
|
|
PRODUCTS = [
|
|
|
|
|
{"id": 1, "name": "商品A", "price": 100},
|
|
|
|
|
{"id": 2, "name": "商品B", "price": 200},
|
|
|
|
|
# 添加更多商品...
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# 购物车数据结构,使用字典方便统计商品数量
|
|
|
|
|
SHOPPING_CART = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ShoppingCartSystem(tk.Tk):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.title("购物车系统")
|
|
|
|
|
self.geometry("400x300")
|
|
|
|
|
|
|
|
|
|
self.create_menu()
|
|
|
|
|
|
|
|
|
|
def create_menu(self):
|
|
|
|
|
buttons_frame = tk.Frame(self)
|
|
|
|
|
buttons_frame.pack(pady=20)
|
|
|
|
|
|
|
|
|
|
tk.Button(buttons_frame, text="注册", command=self.register).pack(pady=5)
|
|
|
|
|
tk.Button(buttons_frame, text="登录", command=self.login).pack(pady=5)
|
|
|
|
|
tk.Button(buttons_frame, text="购物", command=self.shopping).pack(pady=5)
|
|
|
|
|
tk.Button(buttons_frame, text="结账", command=self.checkout).pack(pady=5)
|
|
|
|
|
tk.Button(buttons_frame, text="退货", command=self.return_goods).pack(pady=5)
|
|
|
|
|
tk.Button(buttons_frame, text="消费记录", command=self.shopping_history).pack(pady=5)
|
|
|
|
|
tk.Button(buttons_frame, text="退出", command=self.quit).pack(pady=5)
|
|
|
|
|
|
|
|
|
|
def register(self):
|
|
|
|
|
# 弹出对话框收集用户输入
|
|
|
|
|
username = simpledialog.askstring("注册", "请输入用户名:")
|
|
|
|
|
password = simpledialog.askstring("注册", "请输入密码:", show='*')
|
|
|
|
|
confirm_password = simpledialog.askstring("注册", "请再次输入密码:", show='*')
|
|
|
|
|
|
|
|
|
|
# 简单的输入验证
|
|
|
|
|
if not username or not password or not confirm_password:
|
|
|
|
|
messagebox.showerror("错误", "所有字段都需要填写!")
|
|
|
|
|
return
|
|
|
|
|
if password != confirm_password:
|
|
|
|
|
messagebox.showerror("错误", "两次输入的密码不一致!")
|
|
|
|
|
return
|
|
|
|
|
if UserDB.user_exists(username):
|
|
|
|
|
messagebox.showerror("错误", "用户名已存在!")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 保存用户信息到数据库
|
|
|
|
|
UserDB.add_user(username, password)
|
|
|
|
|
messagebox.showinfo("成功", "注册成功!")
|
|
|
|
|
|
|
|
|
|
def login(self):
|
|
|
|
|
# 弹出对话框收集用户输入
|
|
|
|
|
username = simpledialog.askstring("登录", "请输入用户名:")
|
|
|
|
|
password = simpledialog.askstring("登录", "请输入密码:", show='*')
|
|
|
|
|
|
|
|
|
|
# 验证用户名和密码
|
|
|
|
|
if not username or not password:
|
|
|
|
|
messagebox.showerror("错误", "用户名和密码都是必填项!")
|
|
|
|
|
return
|
|
|
|
|
if UserDB.user_login(username, password):
|
|
|
|
|
messagebox.showinfo("成功", f"欢迎,{username}!登录成功。")
|
|
|
|
|
# 实际应用中可能需要在这里实现页面跳转逻辑
|
|
|
|
|
else:
|
|
|
|
|
messagebox.showerror("错误", "用户名或密码错误,请重试。")
|
|
|
|
|
|
|
|
|
|
def shopping(self):
|
|
|
|
|
# 简化处理,展示商品列表并让用户选择商品添加到购物车
|
|
|
|
|
selected_product_id = simpledialog.askinteger("购物", "请选择商品编号(例如:1):", minvalue=1,
|
|
|
|
|
maxvalue=len(PRODUCTS))
|
|
|
|
|
|
|
|
|
|
if selected_product_id is not None:
|
|
|
|
|
selected_product = PRODUCTS[selected_product_id - 1]
|
|
|
|
|
product_info = f"商品名:{selected_product['name']},价格:{selected_product['price']}元"
|
|
|
|
|
|
|
|
|
|
# 提示用户确认是否添加到购物车
|
|
|
|
|
response = messagebox.askyesno("确认添加", f"是否将商品 {selected_product['name']} 添加到购物车?")
|
|
|
|
|
if response:
|
|
|
|
|
# 添加商品到购物车
|
|
|
|
|
if selected_product['id'] in SHOPPING_CART:
|
|
|
|
|
SHOPPING_CART[selected_product['id']]['quantity'] += 1
|
|
|
|
|
else:
|
|
|
|
|
SHOPPING_CART[selected_product['id']] = {'name': selected_product['name'],
|
|
|
|
|
'price': selected_product['price'], 'quantity': 1}
|
|
|
|
|
|
|
|
|
|
messagebox.showinfo("成功", f"已将 {selected_product['name']} 添加到购物车!")
|
|
|
|
|
else:
|
|
|
|
|
messagebox.showinfo("取消", "操作已取消。")
|
|
|
|
|
|
|
|
|
|
def checkout(self):
|
|
|
|
|
if not SHOPPING_CART:
|
|
|
|
|
messagebox.showinfo("提示", "购物车为空,请先添加商品!")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 计算总价
|
|
|
|
|
total_price = sum(item['price'] * item['quantity'] for item in SHOPPING_CART.values())
|
|
|
|
|
|
|
|
|
|
# 展示结账信息
|
|
|
|
|
cart_details = "\n".join(
|
|
|
|
|
[f"{item['name']} x {item['quantity']} = {item['price'] * item['quantity']}元" for item in
|
|
|
|
|
SHOPPING_CART.values()])
|
|
|
|
|
checkout_message = f"您的购物车商品清单:\n{cart_details}\n总计: {total_price}元"
|
|
|
|
|
|
|
|
|
|
# 使用messagebox显示结账信息,这里仅作为演示,实际应用中可能需要更复杂的界面
|
|
|
|
|
response = messagebox.askyesno("结账确认", checkout_message)
|
|
|
|
|
|
|
|
|
|
if response:
|
|
|
|
|
# 清空购物车(实际应用中可能需要在服务器端同步此操作)
|
|
|
|
|
SHOPPING_CART.clear()
|
|
|
|
|
messagebox.showinfo("成功", "结账成功,感谢您的购买!")
|
|
|
|
|
else:
|
|
|
|
|
messagebox.showinfo("取消", "结账已取消。")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def return_goods(self):
|
|
|
|
|
if not SHOPPING_CART:
|
|
|
|
|
messagebox.showinfo("提示", "购物车为空,没有商品可以退货!")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 构建退货选择界面的逻辑
|
|
|
|
|
def on_return_selected():
|
|
|
|
|
# 获取用户选择的索引
|
|
|
|
|
selected_indices = list_box.curselection()
|
|
|
|
|
if not selected_indices:
|
|
|
|
|
messagebox.showinfo("提示", "请至少选择一项商品进行退货!")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 从购物车移除选中的商品
|
|
|
|
|
for index in reversed(selected_indices):
|
|
|
|
|
item_index = int(list_box.get(index).split(' ')[0]) # 假设列表显示为 "序号 商品名 x 数量 = 总价元"
|
|
|
|
|
del SHOPPING_CART[item_index] # 这里直接根据商品ID(序号)移除,实际情况可能需要更复杂的匹配逻辑
|
|
|
|
|
|
|
|
|
|
messagebox.showinfo("成功", "退货成功!已从购物车移除选中商品。")
|
|
|
|
|
return_window.destroy() # 关闭退货窗口
|
|
|
|
|
|
|
|
|
|
return_window = tk.Toplevel(self)
|
|
|
|
|
return_window.title("选择退货商品")
|
|
|
|
|
return_window.geometry("400x300")
|
|
|
|
|
|
|
|
|
|
list_box_scrollbar = tk.Scrollbar(return_window)
|
|
|
|
|
list_box = tk.Listbox(return_window, yscrollcommand=list_box_scrollbar.set)
|
|
|
|
|
list_box.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
|
|
|
|
|
list_box_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
|
|
|
|
|
list_box_scrollbar.config(command=list_box.yview)
|
|
|
|
|
|
|
|
|
|
# 将购物车中的商品添加到列表供选择
|
|
|
|
|
for idx, (item_id, item_data) in enumerate(SHOPPING_CART.items(), start=1):
|
|
|
|
|
item_str = f"{idx} {item_data['name']} x {item_data['quantity']} = {item_data['price'] * item_data['quantity']}元"
|
|
|
|
|
list_box.insert(tk.END, item_str)
|
|
|
|
|
|
|
|
|
|
tk.Button(return_window, text="确认退货", command=on_return_selected).pack(pady=10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def shopping_history(self):
|
|
|
|
|
messagebox.showinfo("提示", "消费记录功能尚未实现")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
app = ShoppingCartSystem()
|
|
|
|
|
app.mainloop()
|