python tkinter mysql实现酒店管理系统中的客房信息管理:该功能可以让用户管理酒店客房的基本信息,如房间号、房间类型、价格等功能的实例代码
时间: 2024-03-25 09:38:44 浏览: 12
好的,以下是一个简单的实现客房信息管理的示例代码:
```python
import tkinter as tk
import mysql.connector
# 连接 MySQL 数据库
mydb = mysql.connector.connect(
host="localhost",
user="username",
password="password",
database="database_name"
)
mycursor = mydb.cursor()
# 创建客房信息表
mycursor.execute("CREATE TABLE IF NOT EXISTS rooms (room_number INT, room_type VARCHAR(255), price INT)")
# 创建主界面
root = tk.Tk()
root.title("客房信息管理系统")
# 创建标签和文本框
label_room_number = tk.Label(root, text="房间号")
label_room_number.grid(row=0, column=0)
entry_room_number = tk.Entry(root)
entry_room_number.grid(row=0, column=1)
label_room_type = tk.Label(root, text="房间类型")
label_room_type.grid(row=1, column=0)
entry_room_type = tk.Entry(root)
entry_room_type.grid(row=1, column=1)
label_price = tk.Label(root, text="价格")
label_price.grid(row=2, column=0)
entry_price = tk.Entry(root)
entry_price.grid(row=2, column=1)
# 创建添加客房信息函数
def add_room():
room_number = entry_room_number.get()
room_type = entry_room_type.get()
price = entry_price.get()
sql = "INSERT INTO rooms (room_number, room_type, price) VALUES (%s, %s, %s)"
val = (room_number, room_type, price)
mycursor.execute(sql, val)
mydb.commit()
entry_room_number.delete(0, tk.END)
entry_room_type.delete(0, tk.END)
entry_price.delete(0, tk.END)
# 创建显示客房信息函数
def show_rooms():
mycursor.execute("SELECT * FROM rooms")
result = mycursor.fetchall()
for i, r in enumerate(result):
tk.Label(root, text=r).grid(row=i+4, column=0)
# 创建按钮
button_add = tk.Button(root, text="添加客房信息", command=add_room)
button_add.grid(row=3, column=0)
button_show = tk.Button(root, text="显示客房信息", command=show_rooms)
button_show.grid(row=3, column=1)
# 运行主循环
root.mainloop()
```
这段代码使用了 Python 的 tkinter 模块创建了一个图形化界面,让用户可以输入客房的基本信息,并且可以将信息添加到 MySQL 数据库中。同时,还提供了一个按钮,可以显示所有客房的信息。当用户点击添加按钮时,程序会将用户输入的信息插入到 MySQL 数据库中,并且清空输入框,以便用户输入下一个客房的信息。当用户点击显示按钮时,程序会从 MySQL 数据库中读取所有客房的信息,并在界面上显示出来。