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()
|
Binary file not shown.
Binary file not shown.
@ -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()
|
Loading…
Reference in new issue