如何基于电芯输出电压值实现Tkinter Label背景色动态切换
问题核心原因
- 你当前导入使用的是
ttk.Label组件,ttk系列组件的样式统一由ttk.Style管理,不支持直接通过bg参数修改背景色,直接设置属性不会生效 - 你每次点击「Read Cells」按钮都会重新创建4个Label对象,重复创建控件不仅会导致冗余,也会出现属性更新不生效、旧控件覆盖等异常问题
最简解决方案
直接改用原生Tkinter的Label组件,支持直接修改背景色属性,更适合入门场景,修改后的完整可运行代码如下:
#!/usr/bin/env python3 import tkinter as tk from tkinter import ttk # 删除了ttk.Label的导入,改用原生tk.Label import time import board from adafruit_ina219 import ADCResolution, BusVoltageRange, INA219 root = tk.Tk() root.title('Equipmake Cell Voltage Tester') root.geometry('800x450+0+0') root.resizable(False, False) root.attributes('-topmost', 1) #root.iconbitmap('./assets/Equipmake.ico') i2c_bus = board.I2C() ina1 = INA219(i2c_bus,addr=0x40) ina2 = INA219(i2c_bus,addr=0x41) ina3 = INA219(i2c_bus,addr=0x42) ina4 = INA219(i2c_bus,addr=0x43) ina1.bus_adc_resolution = ADCResolution.ADCRES_12BIT_32S ina1.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_32S ina1.bus_voltage_range = BusVoltageRange.RANGE_16V ina2.bus_adc_resolution = ADCResolution.ADCRES_12BIT_32S ina2.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_32S ina2.bus_voltage_range = BusVoltageRange.RANGE_16V ina3.bus_adc_resolution = ADCResolution.ADCRES_12BIT_32S ina3.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_32S ina3.bus_voltage_range = BusVoltageRange.RANGE_16V ina4.bus_adc_resolution = ADCResolution.ADCRES_12BIT_32S ina4.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_32S ina4.bus_voltage_range = BusVoltageRange.RANGE_16V message = tk.Label(root, text="Press firmly against the cell then press Read Cells", bg='blue', fg='white', font=('Helvetica', 20) ) message.place(x=700, y=20, width=600, anchor='ne') # 提前创建4个电压显示Label,后续仅更新属性 label1 = tk.Label( root, text="--.--V", font=("Helvetica", 16), bg="gray" ) label1.place(x=190, y=110, width=150, height=100) label2 = tk.Label( root, text="--.--V", font=("Helvetica", 16), bg="gray" ) label2.place(x=460, y=110, width=150, height=100) label3 = tk.Label( root, text="--.--V", font=("Helvetica", 16), bg="gray" ) label3.place(x=190, y=270, width=150, height=100) label4 = tk.Label( root, text="--.--V", font=("Helvetica", 16), bg="gray" ) label4.place(x=460, y=270, width=150, height=100) # 电压转背景色的工具函数,阈值可根据你实际的电芯规格调整 def get_voltage_bg(voltage): # 示例阈值:<3V欠压(红),3V-4.2V正常(绿),>4.2V过压(黄) if voltage < 3.0: return "red" elif 3.0 <= voltage <= 4.2: return "green" else: return "yellow" # 按钮点击回调修改为仅更新Label的文本和背景色 def button_clicked(): bus_voltage1 = ina1.bus_voltage # voltage on V- (load side) shunt_voltage1 = ina1.shunt_voltage # voltage between V+ and V- across the shunt power1 = ina1.power current1 = ina1.current # current in mA bus_voltage2 = ina2.bus_voltage # voltage on V- (load side) shunt_voltage2 = ina2.shunt_voltage # voltage between V+ and V- across the shunt power2 = ina2.power current2 = ina2.current # current in mA bus_voltage3 = ina3.bus_voltage # voltage on V- (load side) shunt_voltage3 = ina3.shunt_voltage # voltage between V+ and V- across the shunt power3 = ina3.power current3 = ina3.current # current in mA bus_voltage4 = ina4.bus_voltage # voltage on V- (load side) shunt_voltage4 = ina4.shunt_voltage # voltage between V+ and V- across the shunt power4 = ina4.power current4 = ina4.current # current in mA # 计算实际电压同时保留数值用于阈值判断 cell1_voltage = bus_voltage1 + shunt_voltage1 cell2_voltage = bus_voltage2 + shunt_voltage2 cell3_voltage = bus_voltage3 + shunt_voltage3 cell4_voltage = bus_voltage4 + shunt_voltage4 # 更新Label的文本和背景色 label1.config(text=f"{cell1_voltage:6.3f}V", bg=get_voltage_bg(cell1_voltage)) label2.config(text=f"{cell2_voltage:6.3f}V", bg=get_voltage_bg(cell2_voltage)) label3.config(text=f"{cell3_voltage:6.3f}V", bg=get_voltage_bg(cell3_voltage)) label4.config(text=f"{cell4_voltage:6.3f}V", bg=get_voltage_bg(cell4_voltage)) button1 = tk.Button(root, text='Read Cells', command=button_clicked, bg='red', fg='white', activebackground='red', font=('Helvetica', 16) ) button1.place(relx=1.0, rely=0.5, width=120, height=300, anchor='e') button2 = tk.Button(root, text='Read Cells', command=button_clicked, bg='red', fg='white', activebackground='red', font=('Helvetica', 16) ) button2.place(relx=0.0, rely=0.5, width=120, height=300, anchor='w') root.mainloop()
你可以根据自己使用的电芯规格,调整get_voltage_bg函数里的电压阈值和对应颜色即可。
内容的提问来源于stack exchange,提问作者Lawrence O'Brien
相关产品推荐
相关产品推荐

