fourth commit

main
123 6 months ago
parent e358b691b3
commit 286b81c678

@ -0,0 +1,60 @@
import tkinter as tk
from tkinter import ttk
import random
import time
class EnvironmentMonitorApp:
def __init__(self, root):
self.root = root
self.root.title("环境监测数据展示")
# 初始化数据
self.temperature = 0
self.humidity = 0
self.light_intensity = 0
self.record_count = 0 # 新增记录计数器
# 创建表格
self.treeview = ttk.Treeview(root, columns=("序号", "参数", ""), show="headings")
self.treeview.column("序号", width=50, anchor=tk.CENTER)
self.treeview.column("参数", width=100, anchor=tk.CENTER)
self.treeview.column("", width=100, anchor=tk.CENTER)
self.treeview.heading("序号", text="序号")
self.treeview.heading("参数", text="参数")
self.treeview.heading("", text="")
self.treeview.pack(padx=10, pady=10)
# 模拟数据更新
self.root.after(5000, self.update_data_and_display) # 每5秒更新一次数据
def simulate_data(self):
"""Simulate environment data."""
self.temperature = random.uniform(20, 30) # 温度范围20°C至30°C
self.humidity = random.uniform(30, 70) # 湿度范围30%至70%
self.light_intensity = random.randint(100, 1000) # 光照强度100至1000勒克斯
self.record_count += 1 # 记录数加一
def update_table(self):
"""Append new data to the table."""
# 插入新数据,记录计数作为第一列
self.treeview.insert("", tk.END, values=(self.record_count, "温度", f"{self.temperature:.2f} °C"))
self.treeview.insert("", tk.END, values=(self.record_count, "湿度", f"{self.humidity:.2f}%"))
self.treeview.insert("", tk.END, values=(self.record_count, "光照强度", f"{self.light_intensity} lux"))
def update_data_and_display(self):
"""Update data and append to table display."""
self.simulate_data()
self.update_table()
# 递归调用以持续更新
self.root.after(5000, self.update_data_and_display)
def main():
root = tk.Tk()
app = EnvironmentMonitorApp(root)
root.mainloop()
if __name__ == "__main__":
main()

@ -11,16 +11,17 @@ class MainPage:
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.pack_forget()
# 显示家居信息页面 # 显示家居信息页面
JiaJuInfoPage(self.root) self.jiaju_page = JiaJuInfoPage(self.root)
class JiaJuInfoPage: class JiaJuInfoPage:
def __init__(self, master): def __init__(self, master):
self.root = master self.root = master
@ -29,7 +30,9 @@ 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()
root.title("智能家居系统-主页面")
app = MainPage(root) app = MainPage(root)
root.mainloop() root.mainloop()

@ -1,37 +0,0 @@
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秒检查一次传感器状态

@ -0,0 +1,95 @@
# -*- coding: utf-8 -*-
import tkinter as tk
from tkinter import ttk
from tkinter import Toplevel
import random
import time
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
class EnvironmentMonitorApp:
def __init__(self, root):
self.root = root
self.root.title("环境监测数据展示")
self.setup_ui()
self.data_records = [] # 保存所有监测记录
self.plot_window = None # 初始化图表窗口变量
self.figure = None # 初始化图表figure变量
self.canvas = None # 初始化图表canvas变量
self.axs = None # 初始化子图变量
self.root.after(5000, self.update_data_and_display) # 每5秒更新一次数据
def setup_ui(self):
self.treeview = ttk.Treeview(self.root, columns=("序号", "参数", ""), show="headings")
self.treeview.column("序号", width=50, anchor=tk.CENTER)
self.treeview.column("参数", width=100, anchor=tk.CENTER)
self.treeview.column("", width=100, anchor=tk.CENTER)
self.treeview.heading("序号", text="序号")
self.treeview.heading("参数", text="参数")
self.treeview.heading("", text="")
self.treeview.pack(padx=10, pady=10)
self.plot_button = ttk.Button(self.root, text="查看走势图", command=self.show_plot_window)
self.plot_button.pack(pady=10)
def simulate_data(self):
self.temperature = random.uniform(20, 30)
self.humidity = random.uniform(30, 70)
self.light_intensity = random.randint(100, 1000)
self.data_records.append({
"序号": len(self.data_records) + 1,
"温度": self.temperature,
"湿度": self.humidity,
"光照强度": self.light_intensity
})
self.update_treeview()
if self.plot_window and self.figure and self.canvas:
self.update_plots()
def update_treeview(self):
# 更新表格数据
self.treeview.delete(*self.treeview.get_children())
for record in self.data_records:
self.treeview.insert("", tk.END, values=(record["序号"], "温度", f"{record['温度']:.2f} °C"))
self.treeview.insert("", tk.END, values=(record["序号"], "湿度", f"{record['湿度']:.2f}%"))
self.treeview.insert("", tk.END, values=(record["序号"], "光照强度", f"{record['光照强度']} lux"))
def update_data_and_display(self):
self.simulate_data()
self.root.after(5000, self.update_data_and_display)
def show_plot_window(self):
if not self.plot_window:
self.plot_window = Toplevel(self.root)
self.plot_window.title("环境数据走势图")
self.figure, self.axs = plt.subplots(3, 1, figsize=(6, 8), sharex=True)
self.figure.suptitle('环境数据实时走势图')
self.axs[0].set_title('温度变化')
self.axs[1].set_title('湿度变化')
self.axs[2].set_title('光照强度变化')
for ax in self.axs:
ax.legend()
ax.grid(True)
self.canvas = FigureCanvasTkAgg(self.figure, master=self.plot_window)
self.canvas.draw()
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
def update_plots(self):
for ax, key in zip(self.axs, ["温度", "湿度", "光照强度"]):
ax.clear() # 清除旧的图表
ax.set_title(f'{key}变化') # 设置子图标题
ax.plot([r['序号'] for r in self.data_records], [r[key] for r in self.data_records], label=key)
ax.legend()
ax.grid(True)
self.canvas.draw_idle() # 更新图表而不重新绘制整个画布
def main():
root = tk.Tk()
app = EnvironmentMonitorApp(root)
root.mainloop()
if __name__ == "__main__":
main()

@ -11,7 +11,6 @@ class LoginPage:
self.root = master self.root = master
self.page = tk.Frame(self.root) self.page = tk.Frame(self.root)
self.page.pack() self.page.pack()
self.root.title('智能家居系统')
self.root.geometry("300x180") self.root.geometry("300x180")
self.username = tk.StringVar() self.username = tk.StringVar()
@ -40,6 +39,6 @@ class LoginPage:
if __name__ == '__main__': if __name__ == '__main__':
root = tk.Tk() root = tk.Tk()
root.title("智能家居系统") root.title("智能家居系统-登录页面")
LoginPage(root) LoginPage(root)
root.mainloop() root.mainloop()
Loading…
Cancel
Save