首页 > 解决方案 > 根据输出值添加变色标签背景

问题描述

我已经尝试了所有我能想到的尝试根据电池输出电压添加变色标签背景。我对 Python 还很陌生,如果这看起来很愚蠢,我深表歉意。

我觉得我错过了一些基本的东西,因为我是自学成才的,我希望这可能是显而易见的我做错了。

#!/usr/bin/env python3
import tkinter as tk
from tkinter import ttk
from tkinter.ttk import 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')

#print("ina219 test")

# measure and display loop
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
    
    # INA219 measure bus voltage on the load side. So PSU voltage = bus_voltage + shunt_voltage
    Cell1 = ("{:6.3f}V".format((bus_voltage1 + shunt_voltage1)))
    Cell2 = ("{:6.3f}V".format((bus_voltage2 + shunt_voltage2)))
    Cell3 = ("{:6.3f}V".format((bus_voltage3 + shunt_voltage3)))
    Cell4 = ("{:6.3f}V".format((bus_voltage4 + shunt_voltage4)))
    
    label1 = Label(
        root,
        text=str(Cell1),
        font=("Helvetica", 16))
    
    label1.place(x=190, y=110, width=150, height=100)
    
    label2 = Label(
        root,
        text=str(Cell2),
        font=("Helvetica", 16))
    
    label2.place(x=460, y=110, width=150, height=100)
    
    label3 = Label(
        root,
        text=str(Cell3),
        font=("Helvetica", 16))
    
    label3.place(x=190, y=270, width=150, height=100)
    
    label4 = Label(
        root,
        text=str(Cell4),
        font=("Helvetica", 16))
    
    label4.place(x=460, y=270, width=150, height=100)
    
    #print(str(Cell1))
    #print(str(Cell2))
    #print(str(Cell3))
    #print(str(Cell4))
    #print("")
    
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()

这段代码作为电压测试器工作,我只想添加条件颜色,这让我困惑了一天。

标签: pythontkintercolorslabel

解决方案


我建议您在函数之外定义标签,然后将它们放在函数内。据我了解,您正在单击一个按钮来更改值。然后,您可以在值更改后(在您调用的函数中)检查您的标签颜色是否必须更改。如果满足条件,则只需输入:

your_label_name.configure(bg="your_new_color")

我还建议您为这些颜色创建变量,然后将它们放入更改中。


推荐阅读