python - 根据输出值添加变色标签背景
问题描述
我已经尝试了所有我能想到的尝试根据电池输出电压添加变色标签背景。我对 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()
这段代码作为电压测试器工作,我只想添加条件颜色,这让我困惑了一天。
解决方案
我建议您在函数之外定义标签,然后将它们放在函数内。据我了解,您正在单击一个按钮来更改值。然后,您可以在值更改后(在您调用的函数中)检查您的标签颜色是否必须更改。如果满足条件,则只需输入:
your_label_name.configure(bg="your_new_color")
我还建议您为这些颜色创建变量,然后将它们放入更改中。
推荐阅读
- c++ - 将 const 函数引用绑定到 lambda
- r - 如何在不考虑顺序的情况下查找字符串是否包含某些字符?
- google-sheets - Google Sheets api 仅适用于我的浏览器
- sql - PowerApps - 如何从库列表中编辑表单导致一个屏幕
- apache-kafka - Kafka会话超时问题
- node.js - Nginx 上游服务器在其中一个关闭时全部关闭
- r - 如何在有条件的 R 中使用 ColSums
- python - 使用多个分隔符按日期对字符串进行排序
- javascript - 即使在关闭 sweetalert2 后背景仍然存在,使 div 元素无法点击
- javascript - 将 HTML 多选输入限制为仅连续元素(仅 +shift,忽略 +ctrl)