|
|
|
@ -8,18 +8,15 @@ import matplotlib.pyplot as plt
|
|
|
|
|
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
|
|
|
|
|
|
|
|
|
class EnvironmentMonitorApp:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.title("空调自动调节系统")
|
|
|
|
|
self.geometry("800x600")
|
|
|
|
|
|
|
|
|
|
# 环境监测实例
|
|
|
|
|
self.env_monitor = EnvironmentMonitor()
|
|
|
|
|
|
|
|
|
|
# 获取初始环境条件
|
|
|
|
|
initial_temp, initial_humid = self.env_monitor.get_current_conditions()
|
|
|
|
|
# 空调调节实例
|
|
|
|
|
self.aircon = AirConditioner(initial_temp, initial_humid)
|
|
|
|
|
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秒更新一次数据
|
|
|
|
|
|
|
|
|
|
# 设置matplotlib的字体以支持中文显示
|
|
|
|
@ -103,35 +100,6 @@ class EnvironmentMonitorApp:
|
|
|
|
|
ax.grid(True)
|
|
|
|
|
self.canvas.draw_idle() # 更新图表而不重新绘制整个画布
|
|
|
|
|
|
|
|
|
|
# 环境监测模拟
|
|
|
|
|
class EnvironmentMonitor:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.temperature = random.uniform(18, 25) # 初始温度范围调整为更合理的室温范围
|
|
|
|
|
self.humidity = random.uniform(30, 60) # 初始湿度范围调整为常见室内湿度范围
|
|
|
|
|
|
|
|
|
|
def get_current_conditions(self):
|
|
|
|
|
return self.temperature, self.humidity
|
|
|
|
|
|
|
|
|
|
# 空调调节模拟
|
|
|
|
|
class AirConditioner:
|
|
|
|
|
def __init__(self, initial_temperature, initial_humidity):
|
|
|
|
|
self.current_temperature = initial_temperature
|
|
|
|
|
self.current_humidity = initial_humidity
|
|
|
|
|
self.min_temperature = 16
|
|
|
|
|
self.max_temperature = 30
|
|
|
|
|
self.min_humidity = 0
|
|
|
|
|
self.max_humidity = 100
|
|
|
|
|
self.comfortable_temperature_range = (20, 24)
|
|
|
|
|
self.comfortable_humidity_range = (40, 60)
|
|
|
|
|
|
|
|
|
|
def adjust(self):
|
|
|
|
|
if not self.comfortable_temperature_range[0] <= self.current_temperature <= self.comfortable_temperature_range[1]:
|
|
|
|
|
self.current_temperature += 1 if self.current_temperature < self.comfortable_temperature_range[0] else -1
|
|
|
|
|
if not self.comfortable_humidity_range[0] <= self.current_humidity <= self.comfortable_humidity_range[1]:
|
|
|
|
|
self.current_humidity += 5 if self.current_humidity < self.comfortable_humidity_range[0] else -5
|
|
|
|
|
|
|
|
|
|
def display_status(self):
|
|
|
|
|
print(f"当前温度: {self.current_temperature:.1f}°C, 当前湿度: {self.current_humidity:.1f}%")
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
root = tk.Tk()
|
|
|
|
|