【发布时间】:2021-11-15 06:29:54
【问题描述】:
我已经尽我所能尝试根据电池输出电压添加变色标签背景。我对 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()
这段代码用作电压测试仪,我只想添加条件颜色,这让我难过了一天。
【问题讨论】:
-
您究竟尝试过哪些不起作用的方法?请提供minimal reproducible example (MRE)。
-
你想改变哪个标签的背景颜色?
-
您好感谢您的回复,我正在尝试更改显示销售电压
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)等标签的背景,我尝试在测量和显示函数中创建一个 IF 语句,然后创建一个单独的函数,其中包含一个 if 语句,但我不知道如何使它工作
标签: python tkinter colors label