third commit

main
123 6 months ago
parent cf497d87cf
commit e358b691b3

@ -1,107 +1,45 @@
import tkinter as tk import tkinter as tk
from tkinter import ttk, messagebox
import pymysql
import tkinter.messagebox
class JiaJuPage: from 主页面 import MainPage
def __init__(self, master): #from 家居信息 import JiaJuPage
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()
class LoginPage:
def __init__(self,master):
self.root = master
self.page = tk.Frame(self.root)
self.page.pack()
self.root.title('智能家居系统')
self.root.geometry("300x180")
self.username = tk.StringVar()
self.password = tk.StringVar()
tk.Label(self.page).grid(row=0, column=0)
tk.Label(self.page, text='账户').grid(row=1,column=0)
tk.Entry(self.page, textvariable=self.username).grid(row=1, column=1)
tk.Label(self.page, text='密码').grid(row=2, column=0)
tk.Entry(self.page, textvariable=self.password).grid(row=2, column=1, pady=10)
tk.Button(self.page, text='登录', command=self.login_check).grid(row=3, column=0, pady=10)
tk.Button(self.page, text='退出', command=root.quit).grid(row=3, column=1, pady=10,stick=tk.E)
def login_check(self):
name = self.username.get()
pwd = self.password.get()
if name == '123' or pwd == '123' :
tkinter.messagebox.showinfo(title='恭喜', message='登录成功')
self.page.destroy()
#JiaJuPage(self.root)
MainPage(self.root)
else:
tkinter.messagebox.showinfo(title='错误', message='账户或密码错误')
if __name__ == '__main__': if __name__ == '__main__':
root = tk.Tk() root = tk.Tk()
app = JiaJuPage(root) root.title("智能家居系统")
root.protocol("WM_DELETE_WINDOW", app.close_conn) LoginPage(root)
root.mainloop() root.mainloop()

@ -1,176 +1,4 @@
import tkinter as tk 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('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 tkinter import messagebox
from 家居信息 import JiaJuPage from 家居信息 import JiaJuPage
@ -181,23 +9,18 @@ class MainPage:
self.page.pack() self.page.pack()
self.root.geometry('600x400') self.root.geometry('600x400')
self.create_page() self.create_page()
def create_page(self): def create_page(self):
# 创建一个标签用于展示简单的家居信息标题 # 创建一个标签用于展示简单的家居信息标题
self.home_info_title = tk.Label(self.page, text="主页面区", font=("Helvetica", 16)) self.home_info_title = tk.Label(self.page, text="主页面区", font=("Helvetica", 16))
self.home_info_title.pack(pady=20) 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 = tk.Button(self.page, text="家居信息", command=self.show_jiaju_info)
self.goto_jiaju_button.pack(pady=10) self.goto_jiaju_button.pack(pady=10)
def show_jiaju_info(self): def show_jiaju_info(self):
# 销毁当前页面 # 销毁当前页面
self.page.destroy() self.page.destroy()
# 显示家居信息页面 # 显示家居信息页面
JiaJuInfoPage(self.root) JiaJuInfoPage(self.root)
class JiaJuInfoPage: class JiaJuInfoPage:
def __init__(self, master): def __init__(self, master):
self.root = master self.root = master
@ -206,19 +29,7 @@ class JiaJuInfoPage:
self.root.geometry('600x400') self.root.geometry('600x400')
JiaJuPage(self.root) JiaJuPage(self.root)
if __name__ == '__main__': if __name__ == '__main__':
root = tk.Tk() root = tk.Tk()
app = MainPage(root) app = MainPage(root)
root.mainloop() 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()

@ -0,0 +1,89 @@
from datetime import datetime
from flask import request, jsonify
from flask import *
from flask_sqlalchemy import SQLAlchemy
import pymysql
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:LH20021212@localhost:3306/智能家居系统'
db = SQLAlchemy(app)
try:
with app.app_context():
# 尝试连接数据库
db.create_all()
print("数据库连接成功!")
except Exception as e:
print(f"数据库连接失败: {str(e)}")
class JiaJu(db.Model):
_tablename_ = 'JiaJu' # 这里应该是__tablename__
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
color = db.Column(db.String(50))
brand = db.Column(db.String(50))
price = db.Column(db.Float, nullable=False)
production_date = db.Column(db.Date, nullable=False)
#增
@app.route('/add_jiaju', methods=['POST'])
def add_jiaju():
data = request.get_json() # 假设前端发送的是JSON数据
new_jiaju = JiaJu(
name=data['name'],
color=data.get('color'), # 如果color可选则使用get
brand=data.get('brand'),
price=data['price'],
production_date=datetime.strptime(data['production_date'], '%Y-%m-%d') # 假定日期格式为YYYY-MM-DD
)
db.session.add(new_jiaju)
db.session.commit()
return jsonify({"message": "家居添加成功"}), 201
#查
@app.route('/jiaju', methods=['GET'])
def get_all_jiaju():
jiaju_list = JiaJu.query.all()
result = [{"id": item.id, "name": item.name, "color": item.color, "brand": item.brand,
"price": item.price, "production_date": item.production_date.strftime('%Y-%m-%d')} for item in jiaju_list]
return jsonify(result)
#获取
@app.route('/jiaju/<int:jiaju_id>', methods=['GET'])
def get_jiaju(jiaju_id):
jiaju = JiaJu.query.get_or_404(jiaju_id)
return jsonify({
"id": jiaju.id,
"name": jiaju.name,
"color": jiaju.color,
"brand": jiaju.brand,
"price": jiaju.price,
"production_date": jiaju.production_date.strftime('%Y-%m-%d')
})
#更
@app.route('/jiaju/<int:jiaju_id>', methods=['PUT'])
def update_jiaju(jiaju_id):
jiaju = JiaJu.query.get_or_404(jiaju_id)
data = request.get_json()
if 'name' in data:
jiaju.name = data['name']
if 'color' in data:
jiaju.color = data['color']
if 'brand' in data:
jiaju.brand = data['brand']
if 'price' in data:
jiaju.price = data['price']
if 'production_date' in data:
jiaju.production_date = datetime.strptime(data['production_date'], '%Y-%m-%d')
db.session.commit()
return jsonify({"message": "家居信息更新成功"})
#删
@app.route('/jiaju/<int:jiaju_id>', methods=['DELETE'])
def delete_jiaju(jiaju_id):
jiaju = JiaJu.query.get_or_404(jiaju_id)
db.session.delete(jiaju)
db.session.commit()
return jsonify({"message": "家居删除成功"})

@ -0,0 +1,239 @@
import tkinter as tk
from tkinter import ttk, messagebox
import pymysql
#from 主页面 import MainPage
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 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 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()

@ -0,0 +1,37 @@
import time
import random
from twilio.rest import Client
# Twilio配置需替换为你的账户SID、Auth Token和手机号码
account_sid = 'your_account_sid'
auth_token = 'your_auth_token'
twilio_phone_number = '+1234567890' # 你的Twilio电话号码
my_phone_number = '+0987654321' # 接收通知的电话号码
client = Client(account_sid, auth_token)
def simulate_sensor():
"""模拟门窗磁传感器,随机返回开或关的状态"""
return random.choice([True, False]) # True表示门关闭False表示门被打开
def check_intrusion(sensor_state):
"""检查是否有入侵事件,并触发报警"""
if not sensor_state:
print("警报!检测到入侵!")
send_alert_sms()
def send_alert_sms():
"""发送短信报警通知"""
message = client.messages.create(
body="您的家庭安全系统检测到异常入侵,请注意!",
from_=twilio_phone_number,
to=my_phone_number
)
print(f"报警短信已发送Message SID: {message.sid}")
if __name__ == "__main__":
print("安全系统启动,正在监控...")
while True:
sensor_state = simulate_sensor()
check_intrusion(sensor_state)
time.sleep(5) # 模拟每5秒检查一次传感器状态
Loading…
Cancel
Save