import tkinter as tk from tkinter import font from udpmsg import * class ElementDisplayApp: def __init__(self, root): self.root = root self.disHVB = msg_disHighVolBusFbMsg() self.disHVBA = msg_disHighAVolBusFbMsg() self.disHVBB = msg_disHighBVolBusFbMsg() self.disLVB = msg_disLowBusFbMsg() # 定义每组的颜色 self.colors = ["#FFC0CB", "#ADD8E6", "#90EE90", "#FFD700"] # 创建自定义字体 self.custom_font = font.Font(family="Arial", size=12, weight='bold') # 创建4个Frame来分组显示元素的值 self.frames = [] for i in range(4): frame = tk.Frame(self.root, bg=self.colors[i], padx=10, pady=10) # frame.pack(padx=10, pady=10, expand=True, fill='both') frame.grid(row=0, column=i, sticky='nsew') # 使用 grid 布局管理器 self.frames.append(frame) # 定义四个字典来存储元素的值 # self.dict1 = {f"元素{i+1}": f"Value {i+1}" for i in range(16)} self.dict1={'燃料电池断路器':'闭合','动力锂电池断路器':'闭合','推进电机断路器':'闭合','仪表锂电池断路器':'闭合','DC/DC5模块断路器':'闭合', '艏部高压配电断路器':'闭合','艏部FT4/5断路器':'闭合','艉舵1/2断路器':'闭合','艉舵3/4断路器':'闭合', 'FB预留断路器':'闭合','TYZ预留断路器':'闭合','预留断路器':'闭合'} # self.dict2 = {f"元素{i+17}": f"Value {i+17}" for i in range(16)} self.dict2 = {'艏部FT1-3断路器':'闭合','启闭机构断路器':'闭合','桅杆舵机断路器':'闭合','XCZ断路器':'闭合', '艏舵断路器':'闭合','敞开电缸断路器':'闭合'} # self.dict3 = {f"元素{i+33}": f"Value {i+33}" for i in range(16)} self.dict3 = {'仪表锂电池断路器':'闭合','艏部低压断路器':'闭合','UNIT4断路器':'闭合','DC/DC1断路器':'闭合', '预留断路器1':'闭合', '预留断路器2':'闭合','艉部应急电池开关':'闭合'} # self.dict4 = {f"元素{i+49}": f"Value {i+49}" for i in range(16)} self.dict4 = {'UNIT1断路器':'闭合','UNIT2断路器':'闭合','UNIT3断路器':'闭合','UNIT5断路器':'闭合','艏部抛载断路器':'闭合', '预留断路器1':'闭合','预留断路器2':'断开','艏部应急电池开关':'闭合'} # 创建Label来显示每个字典中的元素值,并应用自定义字体 self.labels = [] for i, dictionary in enumerate([self.dict1, self.dict2, self.dict3, self.dict4]): for j, (key, value) in enumerate(dictionary.items()): # 根据元素的值选择颜色 if value == "闭合": color = "green" else: color = "red" label = tk.Label(self.frames[i], text=f"{key}: {value}", fg=color, font=self.custom_font, anchor="w") label.grid(row=j//2, column=j%2, padx=5, pady=5) # self.labels.append(label) def update_disp(self): # 更新Label的文本 for i, dictionary in enumerate([self.dict1, self.dict2, self.dict3, self.dict4]): for j, (key, value) in enumerate(dictionary.items()): # 根据元素的值选择颜色 if value == "闭合": color = "green" else: color = "red" # self.labels[i + j].config(text=f"{key}: {value}", fg=color, font=self.custom_font, anchor="w") label = tk.Label(self.frames[i], text=f"{key}: {value}", fg=color, font=self.custom_font, anchor="w") label.grid(row=j//2, column=j%2, padx=5, pady=5) def updata_data(self): if (self.dict1['燃料电池断路器']=='闭合'): self.disHVB.DCB1 = 0x55 else: self.disHVB.DCB1 = 0xAA if (self.dict1['动力锂电池断路器']=='闭合'): self.disHVB.DCB2 = 0x55 else: self.disHVB.DCB2 = 0xAA if (self.dict1['推进电机断路器']=='闭合'): self.disHVB.ZK1 = 0x55 else: self.disHVB.ZK1 = 0xAA if (self.dict1['仪表锂电池断路器']=='闭合'): self.disHVB.ZK2 = 0x55 else: self.disHVB.ZK2 = 0xAA if (self.dict1['DC/DC5模块断路器']=='闭合'): self.disHVB.ZK3 = 0x55 else: self.disHVB.ZK3 = 0xAA if (self.dict1['艏部高压配电断路器']=='闭合'): self.disHVB.ZK4 = 0x55 else: self.disHVB.ZK4 = 0xAA if (self.dict1['艏部FT4/5断路器']=='闭合'): self.disHVB.ZK5 = 0x55 else: self.disHVB.ZK5 = 0xAA if (self.dict1['艉舵1/2断路器']=='闭合'): self.disHVB.ZK6 = 0x55 else: self.disHVB.ZK6 = 0xAA if (self.dict1['艉舵3/4断路器']=='闭合'): self.disHVB.ZK7 = 0x55 else: self.disHVB.ZK7 = 0xAA if (self.dict1['FB预留断路器']=='闭合'): self.disHVB.ZK8 = 0x55 else: self.disHVB.ZK8 = 0xAA if (self.dict1['TYZ预留断路器']=='闭合'): self.disHVB.ZK9 = 0x55 else: self.disHVB.ZK9 = 0xAA if (self.dict1['预留断路器']=='闭合'): self.disHVB.ZK10 = 0x55 else: self.disHVB.ZK10 = 0xAA ###################################################### if (self.dict2['艏部FT1-3断路器']=='闭合'): self.disHVBA.bowFTDevice123CircuitBreaker = 0x55 else: self.disHVBA.bowFTDevice123CircuitBreaker = 0xAA if (self.dict2['启闭机构断路器']=='闭合'): self.disHVBA.actuatorCircuitBreaker = 0x55 else: self.disHVBA.actuatorCircuitBreaker = 0xAA if (self.dict2['桅杆舵机断路器']=='闭合'): self.disHVBA.mastSteeringGearControlBoxCircuitBreaker = 0x55 else: self.disHVBA.mastSteeringGearControlBoxCircuitBreaker = 0xAA if (self.dict2['XCZ断路器']=='闭合'): self.disHVBA.xczCircuitBreaker = 0x55 else: self.disHVBA.xczCircuitBreaker = 0xAA if (self.dict2['艏舵断路器']=='闭合'): self.disHVBA.bowRudderControlBoxCircuitBreaker = 0x55 else: self.disHVBA.bowRudderControlBoxCircuitBreaker = 0xAA if (self.dict2['敞开电缸断路器']=='闭合'): self.disHVBA.openWaterCoverStartCylinderCircuitBreaker = 0x55 else: self.disHVBA.openWaterCoverStartCylinderCircuitBreaker = 0xAA #################################################################### if (self.dict3['仪表锂电池断路器']=='闭合'): self.disHVBB.lithiumBatteryGroupInstrumentCircuitBreaker = 0x55 else: self.disHVBB.lithiumBatteryGroupInstrumentCircuitBreaker = 0xAA if (self.dict3['艏部低压断路器']=='闭合'): self.disHVBB.bowLowVoltageDistributionCircuitBreaker = 0x55 else: self.disHVBB.bowLowVoltageDistributionCircuitBreaker = 0xAA if (self.dict3['UNIT4断路器']=='闭合'): self.disHVBB.unit4CircuitBreaker = 0x55 else: self.disHVBB.unit4CircuitBreaker = 0xAA if (self.dict3['DC/DC1断路器']=='闭合'): self.disHVBB.dcdc1CircuitBreaker = 0x55 else: self.disHVBB.dcdc1CircuitBreaker = 0xAA if (self.dict3['预留断路器1']=='闭合'): self.disHVBB.reservedCircuitBreaker1 = 0x55 else: self.disHVBB.reservedCircuitBreaker1 = 0xAA if (self.dict3['预留断路器2']=='闭合'): self.disHVBB.reservedCircuitBreaker2 = 0x55 else: self.disHVBB.reservedCircuitBreaker2 = 0xAA if (self.dict3['艉部应急电池开关']=='闭合'): self.disHVBB.emergencyLithiumBatteryGroup2CircuitBreaker = 0x55 else: self.disHVBB.emergencyLithiumBatteryGroup2CircuitBreaker = 0xAA ###################################################################### if (self.dict4['UNIT1断路器']=='闭合'): self.disLVB.unit1CircuitBreaker = 0x55 else: self.disLVB.unit1CircuitBreaker = 0xAA if (self.dict4['UNIT2断路器']=='闭合'): self.disLVB.unit2CircuitBreaker = 0x55 else: self.disLVB.unit2CircuitBreaker = 0xAA if (self.dict4['UNIT3断路器']=='闭合'): self.disLVB.unit3CircuitBreaker = 0x55 else: self.disLVB.unit3CircuitBreaker = 0xAA if (self.dict4['UNIT5断路器']=='闭合'): self.disLVB.unit5CircuitBreaker = 0x55 else: self.disLVB.unit5CircuitBreaker = 0xAA if (self.dict4['预留断路器1']=='闭合'): self.disLVB.reservedCircuitBreaker1 = 0x55 else: self.disLVB.reservedCircuitBreaker1 = 0xAA if (self.dict4['预留断路器2']=='闭合'): self.disLVB.reservedCircuitBreaker2 = 0x55 else: self.disLVB.reservedCircuitBreaker2 = 0xAA if (self.dict4['艏部抛载断路器']=='闭合'): self.disLVB.bowPZDeviceCircuitBreaker = 0x55 else: self.disLVB.bowPZDeviceCircuitBreaker = 0xAA if (self.dict4['艏部应急电池开关']=='闭合'): self.disLVB.emergencyLithiumBatteryGroup1CircuitBreaker = 0x55 else: self.disLVB.emergencyLithiumBatteryGroup1CircuitBreaker = 0xAA a = self.disHVB.pack() b = self.disHVBA.pack() c = self.disHVBB.pack() d = self.disLVB.pack() return a,b,c,d if __name__ == "__main__": root = tk.Tk() app = ElementDisplayApp(root) root.mainloop() # # 创建主窗口 # root = tk.Tk() # # 创建应用实例并在root中显示 # app = ElementDisplayApp(root) # # 运行主循环 # root.mainloop()