import tkinter as tk from tkinter import ttk import random import time # 全局变量 comfortable_temperature_range = (20, 24) comfortable_humidity_range = (40, 60) current_temperature = 0 current_humidity = 0 line_number = 0 update_interval = 5000 # 每5秒更新一次 root = None treeview = None def reset_conditions(): global current_temperature, current_humidity current_temperature = random.uniform(-12, 42) current_humidity = random.uniform(0, 100) def adjust(): global current_temperature, current_humidity reset_conditions() if not comfortable_temperature_range[0] <= current_temperature <= comfortable_temperature_range[1]: current_temperature = random.uniform(*comfortable_temperature_range) if not comfortable_humidity_range[0] <= current_humidity <= comfortable_humidity_range[1]: current_humidity = random.uniform(*comfortable_humidity_range) return current_temperature, current_humidity def schedule_update(): """计划下一次更新""" global line_number line_number += 1 initial_temp, initial_humidity = current_temperature, current_humidity adjusted_temp, adjusted_humidity = adjust() treeview.insert("", tk.END, values=(line_number, f"{initial_temp:.2f}°C", f"{initial_humidity:.2f}%", f"{adjusted_temp:.2f}°C", f"{adjusted_humidity:.2f}%")) # 安排下一次更新 root.after(update_interval, schedule_update) def main(): global root, treeview root = tk.Tk() root.title("智能家居系统-空调自动调节系统") root.geometry("800x600") treeview = ttk.Treeview(root, columns=("编号", "初始温度", "初始湿度", "调节后温度", "调节后湿度"), show="headings") treeview.heading("编号", text="编号") treeview.heading("初始温度", text="初始温度") treeview.heading("初始湿度", text="初始湿度") treeview.heading("调节后温度", text="调节后温度") treeview.heading("调节后湿度", text="调节后湿度") for col in treeview['columns']: treeview.column(col, width=80, anchor=tk.CENTER) treeview.pack(fill=tk.BOTH, expand=True, padx=10, pady=10) root.rowconfigure(0, weight=1) root.columnconfigure(0, weight=1) schedule_update() # 安排首次更新 root.mainloop() if __name__ == "__main__": main()