You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
182 lines
7.2 KiB
182 lines
7.2 KiB
6 months ago
|
import tkinter as tk
|
||
|
import pymysql
|
||
|
from tkinter import messagebox
|
||
|
|
||
|
|
||
|
class JiaJuPage:
|
||
|
def __init__(self, master):
|
||
|
self.root = master
|
||
|
self.page = tk.Frame(self.root)
|
||
|
self.page.pack()
|
||
|
self.root.geometry('600x400')
|
||
|
self.create_page()
|
||
|
|
||
|
def create_page(self):
|
||
|
menubar = tk.Menu(self.root)
|
||
|
|
||
|
# 数据库连接配置
|
||
|
self.conn = pymysql.connect(host='localhost', user='root', password='LH20021212', db='智能家居系统',
|
||
|
charset='utf8mb4')
|
||
|
self.cursor = self.conn.cursor()
|
||
|
|
||
|
menubar.add_command(label='录入家居', command=self.add_jiaju)
|
||
|
menubar.add_command(label='查询家居', command=self.query_jiaju)
|
||
|
menubar.add_command(label='删除家居', command=self.delete_jiaju)
|
||
|
menubar.add_command(label='修改家居', command=self.update_jiaju)
|
||
|
menubar.add_separator() # 添加分隔线使菜单更加清晰
|
||
|
menubar.add_command(label='退出', command=self.exit_app) # 新增退出功能
|
||
|
|
||
|
self.root.config(menu=menubar)
|
||
|
|
||
|
def exit_app(self):
|
||
|
"""退出应用程序的函数"""
|
||
|
if messagebox.askyesno("退出确认", "确定要退出吗?"):
|
||
|
self.close_conn() # 确保关闭数据库连接
|
||
|
self.root.destroy() # 关闭主窗口
|
||
|
|
||
|
def add_jiaju(self):
|
||
|
def submit():
|
||
|
# 获取用户输入
|
||
|
name = entry_name.get()
|
||
|
color = entry_color.get()
|
||
|
brand = entry_brand.get()
|
||
|
price = entry_price.get()
|
||
|
production_date = entry_production_date.get()
|
||
|
# 插入数据库操作
|
||
|
sql = f"INSERT INTO jia_ju(name, color, brand, price, production_date) VALUES ('{name}', '{color}', '{brand}', {price}, '{production_date}')"
|
||
|
try:
|
||
|
self.cursor.execute(sql)
|
||
|
self.conn.commit()
|
||
|
messagebox.showinfo("成功", "家居信息录入成功!")
|
||
|
except Exception as e:
|
||
|
messagebox.showerror("错误", f"录入失败: {e}")
|
||
|
finally:
|
||
|
add_window.destroy() # 关闭对话框
|
||
|
|
||
|
add_window = tk.Toplevel(self.root)
|
||
|
add_window.title("录入家居信息")
|
||
|
tk.Label(add_window, text="名称:").pack()
|
||
|
entry_name = tk.Entry(add_window)
|
||
|
entry_name.pack()
|
||
|
tk.Label(add_window, text="颜色:").pack()
|
||
|
entry_color = tk.Entry(add_window)
|
||
|
entry_color.pack()
|
||
|
tk.Label(add_window, text="品牌:").pack()
|
||
|
entry_brand = tk.Entry(add_window)
|
||
|
entry_brand.pack()
|
||
|
tk.Label(add_window, text="价格:").pack()
|
||
|
entry_price = tk.Entry(add_window)
|
||
|
entry_price.pack()
|
||
|
tk.Label(add_window, text="生产日期:").pack()
|
||
|
entry_production_date = tk.Entry(add_window)
|
||
|
entry_production_date.pack()
|
||
|
tk.Button(add_window, text="提交", command=submit).pack()
|
||
|
|
||
|
def query_jiaju(self):
|
||
|
self.cursor.execute("SELECT * FROM jia_ju")
|
||
|
results = self.cursor.fetchall()
|
||
|
if results:
|
||
|
result_text = "\n".join([str(row) for row in results])
|
||
|
messagebox.showinfo("查询结果", result_text)
|
||
|
else:
|
||
|
messagebox.showinfo("查询结果", "无数据")
|
||
|
|
||
|
def delete_jiaju(self):
|
||
|
def confirm_delete():
|
||
|
item_id = entry_id.get()
|
||
|
# 执行删除操作
|
||
|
sql = f"DELETE FROM jia_ju WHERE id={item_id}"
|
||
|
try:
|
||
|
self.cursor.execute(sql)
|
||
|
self.conn.commit()
|
||
|
messagebox.showinfo("成功", "家居信息已删除!")
|
||
|
except Exception as e:
|
||
|
messagebox.showerror("错误", f"删除失败: {e}")
|
||
|
finally:
|
||
|
delete_window.destroy()
|
||
|
|
||
|
delete_window = tk.Toplevel(self.root)
|
||
|
delete_window.title("删除家居")
|
||
|
tk.Label(delete_window, text="请输入家居ID:").pack()
|
||
|
entry_id = tk.Entry(delete_window)
|
||
|
entry_id.pack()
|
||
|
tk.Button(delete_window, text="确认删除", command=confirm_delete).pack()
|
||
|
|
||
|
def update_jiaju(self):
|
||
|
def fetch_jiaju_info():
|
||
|
item_id = entry_id.get()
|
||
|
sql = f"SELECT * FROM jia_ju WHERE id={item_id}"
|
||
|
try:
|
||
|
self.cursor.execute(sql)
|
||
|
result = self.cursor.fetchone()
|
||
|
if result:
|
||
|
entry_name.delete(0, tk.END)
|
||
|
entry_name.insert(tk.END, result[1])
|
||
|
entry_color.delete(0, tk.END)
|
||
|
entry_color.insert(tk.END, result[2])
|
||
|
entry_brand.delete(0, tk.END)
|
||
|
entry_brand.insert(tk.END, result[3])
|
||
|
entry_price.delete(0, tk.END)
|
||
|
entry_price.insert(tk.END, str(result[4]))
|
||
|
entry_production_date.delete(0, tk.END)
|
||
|
entry_production_date.insert(tk.END, result[5])
|
||
|
else:
|
||
|
messagebox.showinfo("查询结果", "未找到该家居信息")
|
||
|
except Exception as e:
|
||
|
messagebox.showerror("错误", f"查询失败: {e}")
|
||
|
|
||
|
def submit_update():
|
||
|
item_id = entry_id.get()
|
||
|
name = entry_name.get()
|
||
|
color = entry_color.get()
|
||
|
brand = entry_brand.get()
|
||
|
price = entry_price.get()
|
||
|
production_date = entry_production_date.get()
|
||
|
sql = f"UPDATE jia_ju SET name='{name}', color='{color}', brand='{brand}', price={price}, production_date='{production_date}' WHERE id={item_id}"
|
||
|
try:
|
||
|
self.cursor.execute(sql)
|
||
|
self.conn.commit()
|
||
|
messagebox.showinfo("成功", "家居信息已更新!")
|
||
|
except Exception as e:
|
||
|
messagebox.showerror("错误", f"更新失败: {e}")
|
||
|
finally:
|
||
|
update_window.destroy()
|
||
|
|
||
|
update_window = tk.Toplevel(self.root)
|
||
|
update_window.title("修改家居信息")
|
||
|
tk.Label(update_window, text="请输入家居ID:").pack()
|
||
|
entry_id = tk.Entry(update_window)
|
||
|
entry_id.pack()
|
||
|
tk.Button(update_window, text="查询", command=fetch_jiaju_info).pack()
|
||
|
|
||
|
tk.Label(update_window, text="名称:").pack()
|
||
|
entry_name = tk.Entry(update_window)
|
||
|
entry_name.pack()
|
||
|
tk.Label(update_window, text="颜色:").pack()
|
||
|
entry_color = tk.Entry(update_window)
|
||
|
entry_color.pack()
|
||
|
tk.Label(update_window, text="品牌:").pack()
|
||
|
entry_brand = tk.Entry(update_window)
|
||
|
entry_brand.pack()
|
||
|
tk.Label(update_window, text="价格:").pack()
|
||
|
entry_price = tk.Entry(update_window)
|
||
|
entry_price.pack()
|
||
|
tk.Label(update_window, text="生产日期:").pack()
|
||
|
entry_production_date = tk.Entry(update_window)
|
||
|
entry_production_date.pack()
|
||
|
|
||
|
tk.Button(update_window, text="提交修改", command=submit_update).pack()
|
||
|
|
||
|
def close_conn(self):
|
||
|
# 关闭数据库连接
|
||
|
self.cursor.close()
|
||
|
self.conn.close()
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
root = tk.Tk()
|
||
|
app = JiaJuPage(root)
|
||
|
|
||
|
# 确保在程序结束时关闭数据库连接
|
||
|
root.protocol("WM_DELETE_WINDOW", app.close_conn)
|
||
|
root.mainloop()
|