mardi 21 septembre 2021

Adding colour changing label backgrounds based on output value

I have tried everything i can think of to try and add color changing label backgrounds depending on the cell output voltage. I am fairly new to python and apologies if this seems silly.

I feel like I am missing something fundimental, as I am self taught I was hoping it might be something glaringly obvious I am doing wrong.

#!/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()

This code works as a voltage tester I would just like to add the conditional colours, which has had me stumped for a day now.

any pointers and improvemnts are welcome.

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire