You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
7.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import tkinter as tk
from tkinter import ttk
import random
import time
class AirConditioner:
def __init__(self):
self.current_temperature = random.uniform(16, 30)
self.current_humidity = random.uniform(0, 100)
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_temperature(self):
if not self.comfortable_temperature_range[0] <= self.current_temperature <= self.comfortable_temperature_range[
1]:
if self.current_temperature < self.comfortable_temperature_range[0]:
self.current_temperature += 1
elif self.current_temperature > self.comfortable_temperature_range[1]:
self.current_temperature -= 1
def adjust_humidity(self):
if not self.comfortable_humidity_range[0] <= self.current_humidity <= self.comfortable_humidity_range[1]:
if self.current_humidity < self.comfortable_humidity_range[0]:
self.current_humidity += 5
elif self.current_humidity > self.comfortable_humidity_range[1]:
self.current_humidity -= 5
def display_status(self):
print(f"当前温度: {self.current_temperature:.1f}°C, 当前湿度: {self.current_humidity:.1f}%")
def main_loop(self, root): # 接受root参数
if not self.comfortable_temperature_range[0] <= self.current_temperature <= self.comfortable_temperature_range[1]:
self.adjust_temperature()
if not self.comfortable_humidity_range[0] <= self.current_humidity <= self.comfortable_humidity_range[1]:
self.adjust_humidity()
if not (
self.comfortable_temperature_range[0] <= self.current_temperature <= self.comfortable_temperature_range[1]
and
self.comfortable_humidity_range[0] <= self.current_humidity <= self.comfortable_humidity_range[1]
):
self.display_status()
root.after(1000, self.main_loop, root) # 使用after而非sleep
else:
print("温度和湿度均在舒适范围内,停止调节。")
print(f"温度: {self.current_temperature:.1f}°C, 湿度: {self.current_humidity:.1f}%")
# 可能需要在这里调用一次update_gui以最终更新GUI显示结果
# self.app.update_gui_final() 如果在ACApp类中实现了这个方法来显示最终状态
class ACApp(tk.Tk):
def __init__(self):
super().__init__()
self.title("空调自动调节系统")
self.geometry("800x600") # 调整窗口大小以适应内容
# 创建Treeview表格
self.treeview = ttk.Treeview(self, columns=("编号", "初始温度", "初始湿度", "调节后温度", "调节后湿度"),
show="headings")
self.treeview.heading("编号", text="编号")
self.treeview.heading("初始温度", text="初始温度")
self.treeview.heading("初始湿度", text="初始湿度")
self.treeview.heading("调节后温度", text="调节后温度")
self.treeview.heading("调节后湿度", text="调节后湿度")
self.treeview.column("编号", width=50, anchor=tk.CENTER)
self.treeview.column("初始温度", width=80, anchor=tk.CENTER)
self.treeview.column("初始湿度", width=80, anchor=tk.CENTER)
self.treeview.column("调节后温度", width=80, anchor=tk.CENTER)
self.treeview.column("调节后湿度", width=80, anchor=tk.CENTER)
self.treeview.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
self.aircon = AirConditioner()
self.line_number = 1 # 初始化行编号
self.is_adjusting = True # 添加标志位,判断是否仍在调节过程中
self.update_gui() # 先更新一次GUI
self.aircon.main_loop(self) # 传递self给main_loop以便使用after方法
self.treeview.pack(expand=True, fill=tk.BOTH, padx=10, pady=10) # 让Treeview自适应填充空间
self.rowconfigure(0, weight=1) # 让树状视图所在的行填充额外空间
self.columnconfigure(0, weight=1) # 让树状视图所在的列填充额外空间
def update_gui(self):
if self.is_adjusting: # 只在调节过程中更新数据
# 更新Treeview中的数据这里假设初始数据就是当前数据实际应用中可能需要记录调节前的实际初始值
self.treeview.insert("", tk.END, values=(self.line_number,
f"{self.aircon.current_temperature:.1f}°C",
f"{self.aircon.current_humidity:.1f}%",
f"{self.aircon.current_temperature:.1f}°C",
f"{self.aircon.current_humidity:.1f}%"))
self.line_number += 1
else: # 调节完成后,添加最终状态
if self.line_number == 1: # 如果还没有插入过数据,则插入第一行
self.treeview.insert("", tk.END, values=(self.line_number,
f"{self.aircon.current_temperature:.1f}°C",
f"{self.aircon.current_humidity:.1f}%",
f"{self.aircon.current_temperature:.1f}°C",
f"{self.aircon.current_humidity:.1f}%"))
self.line_number += 1
print("温度和湿度均在舒适范围内,停止调节。")
print(f"温度: {self.current_temperature:.1f}°C, 湿度: {self.current_humidity:.1f}%")
self.is_adjusting = False # 设置标志位为False停止调节后的数据更新
def main_loop(self, root):
if self.is_adjusting:
if not self.comfortable_temperature_range[0] <= self.current_temperature <= \
self.comfortable_temperature_range[1]:
self.adjust_temperature()
if not self.comfortable_humidity_range[0] <= self.current_humidity <= self.comfortable_humidity_range[1]:
self.adjust_humidity()
if not (
self.comfortable_temperature_range[0] <= self.current_temperature <=
self.comfortable_temperature_range[1]
and
self.comfortable_humidity_range[0] <= self.current_humidity <= self.comfortable_humidity_range[1]
):
self.display_status()
root.after(1000, self.main_loop, root)
else:
self.is_adjusting = False # 温湿度达到舒适范围后设置标志位为False
else:
self.update_gui() # 最后调用一次update_gui以显示最终状态
if __name__ == "__main__":
app = ACApp()
app.mainloop()