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.

107 lines
4.4 KiB

import tkinter as tk
from tkinter import ttk, messagebox
import pymysql
class JiaJuPage:
def __init__(self, master):
self.root = master
self.root.title("智能家居系统 - 家居管理")
self.root.geometry('1000x700')
self.conn = pymysql.connect(host='localhost', user='root', password='LH20021212', db='智能家居系统',
charset='utf8mb4')
self.cursor = self.conn.cursor()
self.create_widgets()
def create_widgets(self):
# 创建表格框架和滚动条
frame = tk.Frame(self.root)
frame.pack(fill=tk.BOTH, expand=True)
scrollbar_y = tk.Scrollbar(frame, orient=tk.VERTICAL)
scrollbar_x = tk.Scrollbar(frame, orient=tk.HORIZONTAL)
self.tree = ttk.Treeview(frame, columns=("ID", "名称", "颜色", "品牌", "价格", "生产日期"),
yscrollcommand=scrollbar_y.set, xscrollcommand=scrollbar_x.set)
self.tree.heading("#0", text="")
self.tree.heading("ID", text="ID")
self.tree.heading("名称", text="名称")
self.tree.heading("颜色", text="颜色")
self.tree.heading("品牌", text="品牌")
self.tree.heading("价格", text="价格")
self.tree.heading("生产日期", text="生产日期")
self.tree.column("#0", width=0, stretch=tk.NO)
self.tree.column("ID", anchor=tk.CENTER, width=50)
self.tree.column("名称", anchor=tk.CENTER, width=100)
self.tree.column("颜色", anchor=tk.CENTER, width=100)
self.tree.column("品牌", anchor=tk.CENTER, width=100)
self.tree.column("价格", anchor=tk.CENTER, width=100)
self.tree.column("生产日期", anchor=tk.CENTER, width=100)
self.tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar_y.config(command=self.tree.yview)
scrollbar_y.pack(side=tk.RIGHT, fill=tk.Y)
scrollbar_x.config(command=self.tree.xview)
scrollbar_x.pack(side=tk.BOTTOM, fill=tk.X)
# 按钮区域
button_frame = tk.Frame(self.root)
button_frame.pack(pady=10)
tk.Button(button_frame, text="添加家居", command=self.add_jiaju_popup).pack(side=tk.LEFT, padx=10)
tk.Button(button_frame, text="删除选中", command=self.delete_selected_jiaju).pack(side=tk.LEFT, padx=10)
tk.Button(button_frame, text="修改选中", command=self.update_selected_jiaju_popup).pack(side=tk.LEFT, padx=10)
tk.Button(button_frame, text="查询所有", command=self.query_all_jiaju).pack(side=tk.LEFT, padx=10)
tk.Button(button_frame, text="返回主页面", command=self.query_all_jiaju).pack(side=tk.LEFT, padx=10)
query_frame = tk.Frame(self.root)
query_frame.pack(pady=(10, 0)) # 添加一些垂直间隔
tk.Label(query_frame, text="名称:").pack(side=tk.LEFT)
self.entry_name_query = tk.Entry(query_frame)
self.entry_name_query.pack(side=tk.LEFT, padx=(5, 0)) # 为输入框之间添加一点水平间隔
tk.Label(query_frame, text="颜色:").pack(side=tk.LEFT)
self.entry_color_query = tk.Entry(query_frame)
self.entry_color_query.pack(side=tk.LEFT, padx=(5, 0))
tk.Label(query_frame, text="品牌:").pack(side=tk.LEFT)
self.entry_brand_query = tk.Entry(query_frame)
self.entry_brand_query.pack(side=tk.LEFT, padx=(5, 0))
tk.Label(query_frame, text="价格:").pack(side=tk.LEFT)
self.entry_price_query = tk.Entry(query_frame)
self.entry_price_query.pack(side=tk.LEFT, padx=(5, 0))
tk.Label(query_frame, text="生产日期:").pack(side=tk.LEFT)
self.entry_production_date_query = tk.Entry(query_frame)
self.entry_production_date_query.pack(side=tk.LEFT)
tk.Button(query_frame, text="查询", command=self.query_jiaju_condition).pack(side=tk.LEFT, padx=10)
self.query_all_jiaju() # 初始化加载所有数据
def query_all_jiaju(self):
self.tree.delete(*self.tree.get_children())
self.cursor.execute("SELECT * FROM jia_ju")
rows = self.cursor.fetchall()
for row in rows:
self.tree.insert('', tk.END, values=row)
def close_conn(self):
self.cursor.close()
self.conn.close()
self.root.destroy()
if __name__ == '__main__':
root = tk.Tk()
app = JiaJuPage(root)
root.protocol("WM_DELETE_WINDOW", app.close_conn)
root.mainloop()