python 一个简单的点餐系统
import tkinter as tk from tkinter import messagebox import sqlite3 class RestaurantApp: def __init__(self, master): self.master = master self.master.title("餐厅点餐系统") # 连接到数据库 self.connection = sqlite3.connect('menu.db') self.cursor = self.connection.cursor() # 创建菜单表(如果不存在) self.cursor.execute('''CREATE TABLE IF NOT EXISTS menu (id INTEGER PRIMARY KEY, item TEXT, price REAL)''') # 插入初始菜单数据(如果不存在) # self.cursor.execute('''INSERT OR IGNORE INTO menu (item, price) VALUES # ('汉堡', 10), # ('披萨', 15), # ('沙拉', 8), # ('可乐', 3)''') self.connection.commit() # 创建点餐列表 self.order_items = [] # 创建两个按钮 self.customer_button = tk.Button(self.master, text="顾客", command=self.customer_view) self.customer_button.pack() self.employee_button = tk.Button(self.master, text="员工", command=self.employee_view) self.employee_button.pack() def customer_view(self): self.master.withdraw() # 隐藏主窗口 # 创建顾客界面 customer_window = tk.Toplevel(self.master) customer_window.title("顾客界面") # 创建已点菜单列表框 order_frame = tk.Frame(customer_window) order_frame.grid(row=0, column=0) tk.Label(order_frame, text="已点菜单").pack() order_listbox = tk.Listbox(order_frame, height=10, width=50) order_listbox.pack() # 创建删除菜品按钮 def delete_item(): selected_indices = order_listbox.curselection() if selected_indices: item_index = selected_indices[0] order_listbox.delete(item_index) del self.order_items[item_index] else: messagebox.showerror("错误", "请选择要删除的菜品!") delete_button = tk.Button(order_frame, text="删除", command=delete_item) delete_button.pack() # 从数据库中获取菜单信息 self.cursor.execute("SELECT * FROM menu") menu_items = self.cursor.fetchall() # 创建菜单列表 tk.Label(customer_window, text="菜单").grid(row=0, column=1) menu_listbox = tk.Listbox(customer_window, height=10, width=50) menu_listbox.grid(row=1, column=1) for item in menu_items: menu_listbox.insert(tk.END, f"{item[1]} - ¥{item[2]}") # 创建选择数量按钮 def select_quantity(): selected_indices = menu_listbox.curselection() if selected_indices: item_text = menu_listbox.get(selected_indices[0]) # 获取选定菜品文本 item_name = item_text.split(' - ')[0] # 提取菜品名称 ask_quantity(item_name, order_listbox) else: messagebox.showerror("错误", "请选择一个菜品!") quantity_button = tk.Button(customer_window, text="选择数量", command=select_quantity) quantity_button.grid(row=2, column=1) # 创建点餐完成按钮 def finish_order(): # 计算总金额 total_price = sum(self.get_item_price(item[0]) * item[1] for item in self.order_items) # 显示已点菜单和总金额 order_summary = "\n".join([f"{item[0]} x{item[1]}" for item in self.order_items]) messagebox.showinfo("已点菜单", f"您的订单如下:\n{order_summary}\n总金额:¥{total_price}") # 退出程序 self.master.quit() finish_button = tk.Button(customer_window, text="完成点餐", command=finish_order) finish_button.grid(row=3, column=1) def get_item_price(self, item_name): # 根据菜品名称从数据库中获取价格 self.cursor.execute("SELECT price FROM menu WHERE item=?", (item_name,)) price = self.cursor.fetchone() return price[0] if price else 0 def employee_view(self): # 创建员工登录界面 employee_window = tk.Toplevel(self.master) employee_window.title("员工登录") tk.Label(employee_window, text="用户名:").grid(row=0, column=0) username_entry = tk.Entry(employee_window) username_entry.grid(row=0, column=1) tk.Label(employee_window, text="密码:").grid(row=1, column=0) password_entry = tk.Entry(employee_window, show="*") password_entry.grid(row=1, column=1) def login(): username = username_entry.get() password = password_entry.get() # 这里可以添加从数据库中验证用户名和密码的逻辑 if username == "admin" and password == "admin": employee_window.destroy() self.show_menu_management() else: messagebox.showerror("错误", "用户名或密码错误!") login_button = tk.Button(employee_window, text="登录", command=login) login_button.grid(row=2, columnspan=2) def show_menu_management(self): # 创建菜单管理界面 management_window = tk.Toplevel(self.master) management_window.title("菜单管理") # 从数据库中获取菜单信息 self.cursor.execute("SELECT * FROM menu") menu_items = self.cursor.fetchall() # 显示当前菜单 tk.Label(management_window, text="当前菜单").grid(row=0, columnspan=2) menu_listbox = tk.Listbox(management_window, height=5, width=30) menu_listbox.grid(row=1, columnspan=2) for item in menu_items: menu_listbox.insert(tk.END, f"{item[1]} - ¥{item[2]}") # 添加菜品功能 tk.Label(management_window, text="添加菜品").grid(row=2, column=0) new_item_entry = tk.Entry(management_window) new_item_entry.grid(row=2, column=1) tk.Label(management_window, text="价格").grid(row=3, column=0) new_price_entry = tk.Entry(management_window) new_price_entry.grid(row=3, column=1) def add_item(): new_item = new_item_entry.get() new_price = new_price_entry.get() if new_item and new_price: try: price = float(new_price) # 将新菜品添加到数据库 self.cursor.execute("INSERT INTO menu (item, price) VALUES (?, ?)", (new_item, price)) self.connection.commit() menu_listbox.insert(tk.END, f"{new_item} - ¥{price}") messagebox.showinfo("成功", "菜品添加成功!") except ValueError: messagebox.showerror("错误", "请输入有效的价格!") else: messagebox.showerror("错误", "请输入菜品名称和价格!") add_button = tk.Button(management_window, text="添加", command=add_item) add_button.grid(row=4, columnspan=2) def ask_quantity(item_name, order_listbox): # 创建询问数量的窗口 quantity_window = tk.Toplevel() quantity_window.title("选择数量") tk.Label(quantity_window, text=f"请选择{item_name}的数量:").pack() quantity_entry = tk.Entry(quantity_window) quantity_entry.pack() # 创建确定按钮 def confirm_quantity(): quantity = int(quantity_entry.get()) # 在这里可以处理数量的逻辑,例如添加到订单中 order_listbox.insert(tk.END, f"{item_name} x{quantity}") app.order_items.append((item_name, quantity)) # 将菜品添加到订单列表 quantity_window.destroy() confirm_button = tk.Button(quantity_window, text="确定", command=confirm_quantity) confirm_button.pack() # 创建取消按钮 cancel_button = tk.Button(quantity_window, text="取消", command=quantity_window.destroy) cancel_button.pack() # 创建主窗口 root = tk.Tk() app = RestaurantApp(root) root.mainloop()