main
123 6 months ago
parent da7605a2e3
commit 905b38c36a

@ -13,7 +13,17 @@ class AirConditioner:
self.max_humidity = 100 self.max_humidity = 100
self.comfortable_temperature_range = (20, 24) # 舒适温度范围 self.comfortable_temperature_range = (20, 24) # 舒适温度范围
self.comfortable_humidity_range = (40, 60) # 舒适湿度范围 self.comfortable_humidity_range = (40, 60) # 舒适湿度范围
self.environment_variation = True # 控制环境波动开关
def simulate_environment_change(self):
if self.environment_variation:
# 模拟温度和湿度的随机微小波动
self.current_temperature += random.uniform(-1, 1)
self.current_humidity += random.uniform(-5, 5)
# 确保波动后的值仍在合理范围内
self.current_temperature = max(min(self.current_temperature, self.max_temperature), self.min_temperature)
self.current_humidity = max(min(self.current_humidity, self.max_humidity), self.min_humidity)
def adjust_temperature(self): def adjust_temperature(self):
if not self.comfortable_temperature_range[0] <= self.current_temperature <= self.comfortable_temperature_range[ if not self.comfortable_temperature_range[0] <= self.current_temperature <= self.comfortable_temperature_range[
1]: 1]:
@ -107,26 +117,28 @@ class ACApp(tk.Tk):
print(f"温度: {self.current_temperature:.1f}°C, 湿度: {self.current_humidity:.1f}%") print(f"温度: {self.current_temperature:.1f}°C, 湿度: {self.current_humidity:.1f}%")
self.is_adjusting = False # 设置标志位为False停止调节后的数据更新 self.is_adjusting = False # 设置标志位为False停止调节后的数据更新
def main_loop(self, root): def main_loop(self):
if self.is_adjusting: if self.is_adjusting:
if not self.comfortable_temperature_range[0] <= self.current_temperature <= \ # 新增模拟环境变化
self.comfortable_temperature_range[1]: self.aircon.simulate_environment_change()
if not self.comfortable_temperature_range[0] <= self.current_temperature <= self.comfortable_temperature_range[1]:
self.adjust_temperature() self.adjust_temperature()
if not self.comfortable_humidity_range[0] <= self.current_humidity <= self.comfortable_humidity_range[1]: if not self.comfortable_humidity_range[0] <= self.current_humidity <= self.comfortable_humidity_range[1]:
self.adjust_humidity() self.adjust_humidity()
if not ( if not (
self.comfortable_temperature_range[0] <= self.current_temperature <= self.comfortable_temperature_range[0] <= self.current_temperature <= self.comfortable_temperature_range[1]
self.comfortable_temperature_range[1]
and and
self.comfortable_humidity_range[0] <= self.current_humidity <= self.comfortable_humidity_range[1] self.comfortable_humidity_range[0] <= self.current_humidity <= self.comfortable_humidity_range[1]
): ):
self.display_status() self.display_status()
root.after(1000, self.main_loop, root) self.after(1000, self.main_loop) # 继续循环
else: else:
self.is_adjusting = False # 温湿度达到舒适范围后设置标志位为False self.is_adjusting = False
self.update_gui_final() # 假设这里调用最后的更新GUI方法
else: else:
self.update_gui() # 最后调用一次update_gui以显示最终状态 pass # 已经调整完毕,无需操作
if __name__ == "__main__": if __name__ == "__main__":

Loading…
Cancel
Save