在mysql中创建对应的订单数据表和店铺数据表,其中店铺数据表中应包含店铺名,开业状态,菜品名称和菜品价格,之后利用python中的tkinter进行gui设计,结合数据库mysql编写一段代码,要求实现顾客操作系统界面,来实现获取正在营业的店铺菜单功能,点外卖提交订单功能,查询历史订单功能。
时间: 2024-02-24 08:57:28 浏览: 129
好的,以下是一份更新后的代码示例,实现了你所要求的功能:
```python
import tkinter as tk
import mysql.connector
# 连接MySQL数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
# 创建tkinter窗口
window = tk.Tk()
window.title("顾客操作系统界面")
# 查询店铺函数
def get_shops():
# 从数据库中获取正在营业的店铺信息
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM shops WHERE status = 'open'")
shops = mycursor.fetchall()
# 显示店铺信息
label_shop = tk.Label(window, text="店铺名称")
label_shop.grid(column=0, row=0)
for i in range(len(shops)):
label_item = tk.Label(window, text=shops[i][0])
label_item.grid(column=1, row=i+1)
# 创建按钮,查询店铺
button_shop = tk.Button(window, text="获取营业店铺", command=get_shops)
button_shop.grid(column=2, row=0)
# 创建标签和输入框
label_shop = tk.Label(window, text="店铺名称")
label_shop.grid(column=0, row=10)
entry_shop = tk.Entry(window)
entry_shop.grid(column=1, row=10)
# 查询菜单函数
def get_menu():
# 从数据库中获取店铺菜单信息
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM menu WHERE shop_name = %s", (entry_shop.get(),))
menu = mycursor.fetchall()
# 显示菜单信息
label_menu = tk.Label(window, text="菜单")
label_menu.grid(column=0, row=11)
for i in range(len(menu)):
label_item = tk.Label(window, text=menu[i][1] + " " + str(menu[i][2]))
label_item.grid(column=1, row=i+12)
# 创建按钮,查询菜单
button_menu = tk.Button(window, text="获取菜单", command=get_menu)
button_menu.grid(column=2, row=10)
# 创建标签和输入框
label_order = tk.Label(window, text="点单")
label_order.grid(column=0, row=20)
entry_order = tk.Entry(window)
entry_order.grid(column=1, row=20)
# 提交订单函数
def submit_order():
# 将订单信息插入数据库
mycursor = mydb.cursor()
sql = "INSERT INTO orders (shop_name, order_item) VALUES (%s, %s)"
val = (entry_shop.get(), entry_order.get())
mycursor.execute(sql, val)
mydb.commit()
# 显示提交成功信息
label_success = tk.Label(window, text="订单提交成功!")
label_success.grid(column=1, row=21)
# 创建按钮,提交订单
button_submit = tk.Button(window, text="提交订单", command=submit_order)
button_submit.grid(column=2, row=20)
# 创建标签和输入框
label_history = tk.Label(window, text="历史订单")
label_history.grid(column=0, row=30)
entry_history = tk.Entry(window)
entry_history.grid(column=1, row=30)
# 查询历史订单函数
def get_history():
# 从数据库中获取历史订单信息
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM orders WHERE shop_name = %s", (entry_shop.get(),))
history = mycursor.fetchall()
# 显示历史订单信息
label_history = tk.Label(window, text="历史订单")
label_history.grid(column=0, row=31)
for i in range(len(history)):
label_item = tk.Label(window, text=history[i][2])
label_item.grid(column=1, row=i+32)
# 创建按钮,查询历史订单
button_history = tk.Button(window, text="查询历史订单", command=get_history)
button_history.grid(column=2, row=30)
window.mainloop()
```
这份代码在之前的基础上进行了更新,增加了两个数据表:`shops`和`menu`。其中`shops`表中包含了店铺名、开业状态等信息,`menu`表中包含了菜品名称和价格等信息。在查询店铺、获取菜单和查询历史订单的过程中,都需要先输入店铺名称,并通过数据库查询相应的信息。需要注意的是,在实际应用中,还需要加入更多的功能和处理逻辑,以满足实际需求。
阅读全文