From cf497d87cf553cd5edd5c9b0322b52e7caeac349 Mon Sep 17 00:00:00 2001 From: 123 <2070873540@qq.com> Date: Tue, 28 May 2024 23:04:53 +0800 Subject: [PATCH] second commit --- 智能家居系统/1.py | 241 ++++++------------ 智能家居系统/2.py | 229 ++++++++++++++++- .../__pycache__/主页面.cpython-311.pyc | Bin 1855 -> 2872 bytes .../__pycache__/家居信息.cpython-311.pyc | Bin 1887 -> 20085 bytes 智能家居系统/user_data.json | 1 - 智能家居系统/user_info.txt | 0 智能家居系统/主页面.py | 23 +- 智能家居系统/增删改查窗口.html | 32 --- 智能家居系统/家居信息.py | 237 ++++++++++------- 9 files changed, 469 insertions(+), 294 deletions(-) delete mode 100644 智能家居系统/user_data.json delete mode 100644 智能家居系统/user_info.txt delete mode 100644 智能家居系统/增删改查窗口.html diff --git a/智能家居系统/1.py b/智能家居系统/1.py index d2aa9ae..f015e0a 100644 --- a/智能家居系统/1.py +++ b/智能家居系统/1.py @@ -1,182 +1,107 @@ import tkinter as tk +from tkinter import ttk, messagebox 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() + self.root.title("智能家居系统 - 家居管理") + self.root.geometry('1000x700') - 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.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") - 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() + 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() \ No newline at end of file diff --git a/智能家居系统/2.py b/智能家居系统/2.py index 6f5404f..4edd2a2 100644 --- a/智能家居系统/2.py +++ b/智能家居系统/2.py @@ -1,7 +1,224 @@ -from 测试 import db, Jia +import tkinter as tk +from tkinter import ttk, messagebox +import pymysql -s1 = Jia(编号=1, 名称='电脑', 品牌='联想', 颜色='黑色', 价格='5000', 生产日期='2020-01-01') -s2 = Jia(编号=2, 名称='冰箱', 品牌='联想', 颜色='白色', 价格='6000', 生产日期='2020-01-02') -s3 = Jia(编号=2, 名称='电视机', 品牌='联想', 颜色='黑色', 价格='3000', 生产日期='2020-01-02') -db.session.add(s) -db.session.commit() \ No newline at end of file + +class JiaJuPage: + def __init__(self, master): + self.root = master + self.root.title("智能家居系统 - 家居管理") + self.root.geometry('800x700') + + 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) + + 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 add_jiaju_popup(self): + def submit_add(): + 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("成功", "家居信息录入成功!") + add_window.destroy() # 关闭弹窗后刷新列表 + self.query_all_jiaju() + except Exception as e: + messagebox.showerror("错误", f"录入失败: {e}") + + 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_add).pack() + + def delete_selected_jiaju(self): + selected_items = self.tree.selection() + if not selected_items: + messagebox.showwarning("警告", "请先选择要删除的家居项!") + return + for item in selected_items: + item_id = self.tree.item(item)['values'][0] + sql = f"DELETE FROM jia_ju WHERE id={item_id}" + try: + self.cursor.execute(sql) + self.conn.commit() + except Exception as e: + messagebox.showerror("错误", f"删除失败: {e}") + self.conn.rollback() + return + messagebox.showinfo("成功", "选中的家居信息已删除!") + self.query_all_jiaju() # 刷新列表 + + def update_selected_jiaju_popup(self): + selected_items = self.tree.selection() + if not selected_items: + messagebox.showwarning("警告", "请先选择要修改的家居项!") + return + + item = selected_items[0] # 假设一次只修改一项 + item_values = self.tree.item(item)['values'] + item_id = item_values[0] + + def submit_update(): + 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("成功", "家居信息已更新!") + update_window.destroy() # 关闭弹窗后刷新列表 + self.query_all_jiaju() + except Exception as e: + messagebox.showerror("错误", f"更新失败: {e}") + + update_window = tk.Toplevel(self.root) + update_window.title("修改家居信息") + tk.Label(update_window, text="名称:").pack() + entry_name = tk.Entry(update_window) + entry_name.insert(tk.END, item_values[1]) # 预填充现有值 + entry_name.pack() + tk.Label(update_window, text="颜色:").pack() + entry_color = tk.Entry(update_window) + entry_color.insert(tk.END, item_values[2]) + entry_color.pack() + tk.Label(update_window, text="品牌:").pack() + entry_brand = tk.Entry(update_window) + entry_brand.insert(tk.END, item_values[3]) + entry_brand.pack() + tk.Label(update_window, text="价格:").pack() + entry_price = tk.Entry(update_window) + entry_price.insert(tk.END, item_values[4]) + entry_price.pack() + tk.Label(update_window, text="生产日期:").pack() + entry_production_date = tk.Entry(update_window) + entry_production_date.insert(tk.END, item_values[5]) + entry_production_date.pack() + tk.Button(update_window, text="提交修改", command=submit_update).pack() + + + + def close_conn(self):import tkinter as tk +from tkinter import messagebox +from 家居信息 import JiaJuPage + +class MainPage: + 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): + # 创建一个标签用于展示简单的家居信息标题 + self.home_info_title = tk.Label(self.page, text="主页面区", font=("Helvetica", 16)) + self.home_info_title.pack(pady=20) + + # 添加一个按钮用于跳转到家居信息页面 + self.goto_jiaju_button = tk.Button(self.page, text="家居信息", command=self.show_jiaju_info) + self.goto_jiaju_button.pack(pady=10) + + def show_jiaju_info(self): + # 销毁当前页面 + self.page.destroy() + # 显示家居信息页面 + JiaJuInfoPage(self.root) + + +class JiaJuInfoPage: + def __init__(self, master): + self.root = master + self.page = tk.Frame(self.root) + self.page.pack() + self.root.geometry('600x400') + JiaJuPage(self.root) + + + +if __name__ == '__main__': + root = tk.Tk() + app = MainPage(root) + root.mainloop() + 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() \ No newline at end of file diff --git a/智能家居系统/__pycache__/主页面.cpython-311.pyc b/智能家居系统/__pycache__/主页面.cpython-311.pyc index 7fc05826844a99a9a3017e394cfd74b246720937..d040441d586b9197a687acd1b89fb7016bd0d733 100644 GIT binary patch delta 1360 zcmZWoO=uHA6rS1aZYDKNZPn1&s!5~8t@RJ|;1B*?MA0HtQQ~1u-EEUL$(GqIY7wfS z;K75kLa!A>Es7$7c(NB21P>btSQdmHtl%xhp2UOi%_g-qv$NmM`V`uf;peNstwHH2uG)W9%&;rR&_(}y*lrj=UU8oQ$${Cpwi5w?}e2W;$V~LPw z@CGBJ2#X4q@P8K73>$1jc=BBmG3hXKr)a!069GXnBabO;1aEDlCNna%%3Yvq+$Xek zn?d{GMEyjI{6u@7M*XIuY1`Q$bHKU^QsiVldvfdy$oJ>s-AVA7B&J9pBN_A-$%G6E zP&PtR%Ii3lD4li_(ow*RQjV&x-%GP}Vm*ZW;mcOBm@OGUksyunRj5=>We3ax zZ^m^?&MKOEzBFj*PTnb)Jce5J*qGy3CBMbaSy%Moe0F$D`1q|umSfcv1HxyT@-_&u zG3g{<3&>XkjJunZ_4JCnUpY{kPo*Cvp6+?R@9iP4|9qu?$V=tQsaz$MyCYZF##t5x z{L0c_So&$3$I@liTVcIkeeyW$cX^3a5JI*0*Ckj?lIvs`zQLHHb-E`UrLH9CfZ@JRbKIbbAgn6|^MYksq+#tEPXE5eaMz6D@;s%enN+fQfnCG^>`oUS7fxCY&O z0d9~v7QGw!$`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A){3=0<>3$!9GW^dX0v17-wnJ~k4IVA42vz={YhqMmZ5T^BG^*V$j;k6Ijv|J ztjKF}>eN54;@`Qq<_e3}$7a63%(RiT=Jqb#di2Ge_Vr)ezpOmGdHdn*D|7B*_wM(5 z7jKLg7Hz7kseH;()uph1I^!P3YgU{?QGr$@R+UzJ??ql*dw=8n<&PSXv1Vk9LoGV6 z+WYO$*Fy~&ZPI9+Mjd2_#+j0G6VC=ezdGrrQV#vE{6G!N@w6y(^oKZv5a{TxzxU_|lI7VE69z zy|v#1Kzrrp?yYb9U{cP=mlT;%e2*Ni#p6T2KTw#BCw& zb3yZ3nVmq84Wd|j;jkqGSD+tF#Z43yxEGGBjctrvRvO`vW_Y9;dO)Asp~KsBxHev= z!wovoq!V>I@eH_iH2Zk)1K$5`XKyTE_c!S2COuvE9u~$-rxlssv^?H%Rkiz6HD~Ij zEY}BA^^=m8buH|9i7W^7AE_hQH&!DLIzm6cw!yWL&P@g>3fv1HVjtAk1 z?Rq+cHvNEYWeny-FbKb%DXWSOfXWWCMoSHSw0(%MZ!=(PDZ@YVVhrNsjel3fO zC^{k`qy>?>_jIH%!Hk|3e{t;ZL&GZ|4OEx5#gldMWUK!~^~~z)U!7e&TRGQ~1}h&_ d^@}shGnJW^M8BN+e9B!^5a@(JilK{>+J76-pBVrE diff --git a/智能家居系统/user_data.json b/智能家居系统/user_data.json deleted file mode 100644 index edf164e..0000000 --- a/智能家居系统/user_data.json +++ /dev/null @@ -1 +0,0 @@ -{"lh": "123"} \ No newline at end of file diff --git a/智能家居系统/user_info.txt b/智能家居系统/user_info.txt deleted file mode 100644 index e69de29..0000000 diff --git a/智能家居系统/主页面.py b/智能家居系统/主页面.py index d59f0c1..2db2d56 100644 --- a/智能家居系统/主页面.py +++ b/智能家居系统/主页面.py @@ -1,5 +1,6 @@ import tkinter as tk from tkinter import messagebox +from 家居信息 import JiaJuPage class MainPage: def __init__(self, master): @@ -8,17 +9,25 @@ class MainPage: self.page.pack() self.root.geometry('600x400') self.create_page() - def create_page(self): # 创建一个标签用于展示简单的家居信息标题 self.home_info_title = tk.Label(self.page, text="主页面区", font=("Helvetica", 16)) self.home_info_title.pack(pady=20) - - # 创建一个按钮,点击后显示更详细的家居信息 - self.view_details_button = tk.Button(self.page, text="家居信息", command=self.view_home_details) - self.view_details_button.pack(pady=10) - - + # 添加一个按钮用于跳转到家居信息页面 + self.goto_jiaju_button = tk.Button(self.page, text="家居信息", command=self.show_jiaju_info) + self.goto_jiaju_button.pack(pady=10) + def show_jiaju_info(self): + # 销毁当前页面 + self.page.destroy() + # 显示家居信息页面 + JiaJuInfoPage(self.root) +class JiaJuInfoPage: + def __init__(self, master): + self.root = master + self.page = tk.Frame(self.root) + self.page.pack() + self.root.geometry('600x400') + JiaJuPage(self.root) if __name__ == '__main__': root = tk.Tk() diff --git a/智能家居系统/增删改查窗口.html b/智能家居系统/增删改查窗口.html deleted file mode 100644 index d17cd3c..0000000 --- a/智能家居系统/增删改查窗口.html +++ /dev/null @@ -1,32 +0,0 @@ - - - - 家居管理 - - - - -
- - -
- - - - diff --git a/智能家居系统/家居信息.py b/智能家居系统/家居信息.py index d2aa9ae..a4c829c 100644 --- a/智能家居系统/家居信息.py +++ b/智能家居系统/家居信息.py @@ -1,57 +1,111 @@ import tkinter as tk +from tkinter import ttk, messagebox import pymysql -from tkinter import messagebox - +#from 主页面 import MainPage 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.root.title("智能家居系统 - 家居管理") + self.root.geometry('1000x700') - # 数据库连接配置 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() # 关闭主窗口 + 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 add_jiaju(self): - def submit(): - # 获取用户输入 + def add_jiaju_popup(self): + def submit_add(): 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("成功", "家居信息录入成功!") + add_window.destroy() # 关闭弹窗后刷新列表 + self.query_all_jiaju() except Exception as e: messagebox.showerror("错误", f"录入失败: {e}") - finally: - add_window.destroy() # 关闭对话框 add_window = tk.Toplevel(self.root) add_window.title("录入家居信息") @@ -70,63 +124,37 @@ class JiaJuPage: 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() - # 执行删除操作 + tk.Button(add_window, text="提交", command=submit_add).pack() + + def delete_selected_jiaju(self): + selected_items = self.tree.selection() + if not selected_items: + messagebox.showwarning("警告", "请先选择要删除的家居项!") + return + for item in selected_items: + item_id = self.tree.item(item)['values'][0] 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}") + self.conn.rollback() + return + messagebox.showinfo("成功", "选中的家居信息已删除!") + self.query_all_jiaju() # 刷新列表 + + def update_selected_jiaju_popup(self): + selected_items = self.tree.selection() + if not selected_items: + messagebox.showwarning("警告", "请先选择要修改的家居项!") + return + + item = selected_items[0] # 假设一次只修改一项 + item_values = self.tree.item(item)['values'] + item_id = item_values[0] def submit_update(): - item_id = entry_id.get() name = entry_name.get() color = entry_color.get() brand = entry_brand.get() @@ -137,46 +165,75 @@ class JiaJuPage: self.cursor.execute(sql) self.conn.commit() messagebox.showinfo("成功", "家居信息已更新!") + update_window.destroy() # 关闭弹窗后刷新列表 + self.query_all_jiaju() 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.insert(tk.END, item_values[1]) # 预填充现有值 entry_name.pack() tk.Label(update_window, text="颜色:").pack() entry_color = tk.Entry(update_window) + entry_color.insert(tk.END, item_values[2]) entry_color.pack() tk.Label(update_window, text="品牌:").pack() entry_brand = tk.Entry(update_window) + entry_brand.insert(tk.END, item_values[3]) entry_brand.pack() tk.Label(update_window, text="价格:").pack() entry_price = tk.Entry(update_window) + entry_price.insert(tk.END, item_values[4]) entry_price.pack() tk.Label(update_window, text="生产日期:").pack() entry_production_date = tk.Entry(update_window) + entry_production_date.insert(tk.END, item_values[5]) entry_production_date.pack() - tk.Button(update_window, text="提交修改", command=submit_update).pack() + def query_jiaju_condition(self): + name_cond = self.entry_name_query.get().strip() + color_cond = self.entry_color_query.get().strip() + brand_cond = self.entry_brand_query.get().strip() + price_cond = self.entry_price_query.get().strip() + production_date_cond = self.entry_production_date_query.get().strip() + + conditions = [] + if name_cond: + conditions.append(f"name LIKE '%{name_cond}%'") + if color_cond: + conditions.append(f"color LIKE '%{color_cond}%'") + if brand_cond: + conditions.append(f"brand LIKE '%{brand_cond}%'") + if price_cond.isdigit(): # 确保价格是数字 + conditions.append(f"price = {price_cond}") + if production_date_cond: + conditions.append(f"production_date = '{production_date_cond}'") + + where_clause = ' AND '.join(conditions) if conditions else '1=1' + sql = f"SELECT * FROM jia_ju WHERE {where_clause}" + + try: + self.cursor.execute(sql) + rows = self.cursor.fetchall() + self.tree.delete(*self.tree.get_children()) + for row in rows: + self.tree.insert('', tk.END, values=row) + except Exception as e: + messagebox.showerror("错误", f"查询失败: {e}") + self.conn.rollback() + 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() \ No newline at end of file