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.

67 lines
3.1 KiB

import tkinter as tk
from tkinter import ttk
import random
import time
class AirConditioner:
def __init__(self):
self.comfortable_temperature_range = (20, 24) # 添加舒适温度范围
self.comfortable_humidity_range = (40, 60) # 添加舒适湿度范围
self.reset_conditions()
def reset_conditions(self):
self.current_temperature = random.uniform(-12, 42)
self.current_humidity = random.uniform(0, 100)
def adjust(self):
self.reset_conditions() # 获取新的初始温度和湿度
if not self.comfortable_temperature_range[0] <= self.current_temperature <= self.comfortable_temperature_range[1]:
self.current_temperature = random.uniform(*self.comfortable_temperature_range)
if not self.comfortable_humidity_range[0] <= self.current_humidity <= self.comfortable_humidity_range[1]:
self.current_humidity = random.uniform(*self.comfortable_humidity_range)
return self.current_temperature, self.current_humidity # 返回调节后的温度和湿度
class ACApp(tk.Tk):
def __init__(self):
super().__init__()
self.title("空调自动调节系统")
self.geometry("800x600")
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="调节后湿度")
for col in self.treeview['columns']:
self.treeview.column(col, width=80, anchor=tk.CENTER)
self.treeview.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
self.aircon = AirConditioner()
self.line_number = 0
self.is_adjusted = False
self.update_interval = 5000 # 每5秒更新一次
self.schedule_update() # 安排首次更新
self.treeview.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
def schedule_update(self):
"""计划下一次更新"""
self.line_number += 1
initial_temp, initial_humidity = self.aircon.current_temperature, self.aircon.current_humidity
adjusted_temp, adjusted_humidity = self.aircon.adjust()
self.treeview.insert("", tk.END, values=(self.line_number,
f"{initial_temp:.1f}°C",
f"{initial_humidity:.1f}%",
f"{adjusted_temp:.1f}°C",
f"{adjusted_humidity:.1f}%"))
# 安排下一次更新
self.after(self.update_interval, self.schedule_update)
if __name__ == "__main__":
app = ACApp()
app.mainloop()