[英]How do I loop the label variable in python tkinter?
I have a loop like below to read the voltage values for different devices.我有一个如下所示的循环来读取不同设备的电压值。
for dut in range(TEST_BENCH_CURRENT_NB_DUTS):
if dut_sn[dut].get() == "" :
for voltage_test in range(Initial_voltages):
# Delay between voltage readings
try:
# Read DUT Voltage
time.sleep(1)
voltage = swapBits(busI2C.read_word_data(*I2C_ADDRESSES_1[voltage_test])) >> 4
voltage_readings[dut][voltage_test].set(str(voltage))
window.update()
time.sleep(0.1)
except:
voltage = 0
text = voltage_readings[dut][voltage_test].set(VOLTAGE_READ_PB)
window.update()
minimum_value = float(LIST_TP_VOLTAGE_MIN[voltage_test][7:])
maximum_value = float(LIST_TP_VOLTAGE_MAX[voltage_test][7:])
# Control value is min < value < max
if voltage < minimum_value or maximum_value < voltage :
#label_DUT1_Global_OK_KO_Status = Entry(window, textvariable=voltage_readings[dut][voltage_test], font=(None,TEXT_SIZE), highlightbackground ='red', state=DISABLED, justify=CENTER, width=ENTRY_WIDTH)
And also I have a GUI built with tkinter to display these values.而且我还有一个用 tkinter 构建的 GUI 来显示这些值。
voltage_readings = []
for dut in range(NUM_DUT):
voltage_readings.append([])
for voltage_test in range(Initial_voltages):
voltage_readings[dut].append(StringVar())
label_DUT1_Voltage_Readings_01 = Entry(window, textvariable=voltage_readings[1 - 1][1 - 1], font=(None,TEXT_SIZE), disabledbackground ="#f7f5f5", disabledforeground ="black", state=DISABLED, justify=CENTER, width=ENTRY_WIDTH).place(x=X_LABEL_DUT1-X_LABEL_KEY_PADDING,y=Y_LABEL_FIRST+LIST_SCREEN_ORDER[2]*Y_LABEL_SPACE)
label_DUT2_Voltage_Readings_01 = Entry(window, textvariable=voltage_readings[2 - 1][1 - 1], font=(None,TEXT_SIZE), disabledbackground ="#f7f5f5", disabledforeground ="black", state=DISABLED, justify=CENTER, width=ENTRY_WIDTH).place(x=X_LABEL_DUT2-X_LABEL_KEY_PADDING,y=Y_LABEL_FIRST+LIST_SCREEN_ORDER[2]*Y_LABEL_SPACE)
label_DUT3_Voltage_Readings_01 = Entry(window, textvariable=voltage_readings[3 - 1][1 - 1], font=(None,TEXT_SIZE), disabledbackground ="#f7f5f5", disabledforeground ="black", state=DISABLED, justify=CENTER, width=ENTRY_WIDTH).place(x=X_LABEL_DUT3-X_LABEL_KEY_PADDING,y=Y_LABEL_FIRST+LIST_SCREEN_ORDER[2]*Y_LABEL_SPACE)
label_DUT4_Voltage_Readings_01 = Entry(window, textvariable=voltage_readings[4 - 1][1 - 1], font=(None,TEXT_SIZE), disabledbackground ="#f7f5f5", disabledforeground ="black", state=DISABLED, justify=CENTER, width=ENTRY_WIDTH).place(x=X_LABEL_DUT4-X_LABEL_KEY_PADDING,y=Y_LABEL_FIRST+LIST_SCREEN_ORDER[2]*Y_LABEL_SPACE)
label_DUT1_Voltage_Readings_02 = Entry(window, textvariable=voltage_readings[1 - 1][2 - 1], font=(None,TEXT_SIZE), disabledbackground ="#f7f5f5", disabledforeground ="black", state=DISABLED, justify=CENTER, width=ENTRY_WIDTH).place(x=X_LABEL_DUT1-X_LABEL_KEY_PADDING,y=Y_LABEL_FIRST+LIST_SCREEN_ORDER[3]*Y_LABEL_SPACE)
label_DUT2_Voltage_Readings_02 = Entry(window, textvariable=voltage_readings[2 - 1][2 - 1], font=(None,TEXT_SIZE), disabledbackground ="#f7f5f5", disabledforeground ="black", state=DISABLED, justify=CENTER, width=ENTRY_WIDTH).place(x=X_LABEL_DUT2-X_LABEL_KEY_PADDING,y=Y_LABEL_FIRST+LIST_SCREEN_ORDER[3]*Y_LABEL_SPACE)
label_DUT3_Voltage_Readings_02 = Entry(window, textvariable=voltage_readings[3 - 1][2 - 1], font=(None,TEXT_SIZE), disabledbackground ="#f7f5f5", disabledforeground ="black", state=DISABLED, justify=CENTER, width=ENTRY_WIDTH).place(x=X_LABEL_DUT3-X_LABEL_KEY_PADDING,y=Y_LABEL_FIRST+LIST_SCREEN_ORDER[3]*Y_LABEL_SPACE)
label_DUT4_Voltage_Readings_02 = Entry(window, textvariable=voltage_readings[4 - 1][2 - 1], font=(None,TEXT_SIZE), disabledbackground ="#f7f5f5", disabledforeground ="black", state=DISABLED, justify=CENTER, width=ENTRY_WIDTH).place(x=X_LABEL_DUT4-X_LABEL_KEY_PADDING,y=Y_LABEL_FIRST+LIST_SCREEN_ORDER[3]*Y_LABEL_SPACE)
label_DUT1_Voltage_Readings_03 = Entry(window, textvariable=voltage_readings[1 - 1][3 - 1], font=(None,TEXT_SIZE), disabledbackground ="#f7f5f5", disabledforeground ="black", state=DISABLED, justify=CENTER, width=ENTRY_WIDTH).place(x=X_LABEL_DUT1-X_LABEL_KEY_PADDING,y=Y_LABEL_FIRST+LIST_SCREEN_ORDER[4]*Y_LABEL_SPACE)
label_DUT2_Voltage_Readings_03 = Entry(window, textvariable=voltage_readings[2 - 1][3 - 1], font=(None,TEXT_SIZE), disabledbackground ="#f7f5f5", disabledforeground ="black", state=DISABLED, justify=CENTER, width=ENTRY_WIDTH).place(x=X_LABEL_DUT2-X_LABEL_KEY_PADDING,y=Y_LABEL_FIRST+LIST_SCREEN_ORDER[4]*Y_LABEL_SPACE)
label_DUT3_Voltage_Readings_03 = Entry(window, textvariable=voltage_readings[3 - 1][3 - 1], font=(None,TEXT_SIZE), disabledbackground ="#f7f5f5", disabledforeground ="black", state=DISABLED, justify=CENTER, width=ENTRY_WIDTH).place(x=X_LABEL_DUT3-X_LABEL_KEY_PADDING,y=Y_LABEL_FIRST+LIST_SCREEN_ORDER[4]*Y_LABEL_SPACE)
label_DUT4_Voltage_Readings_03 = Entry(window, textvariable=voltage_readings[4 - 1][3 - 1], font=(None,TEXT_SIZE), disabledbackground ="#f7f5f5", disabledforeground ="black", state=DISABLED, justify=CENTER, width=ENTRY_WIDTH).place(x=X_LABEL_DUT4-X_LABEL_KEY_PADDING,y=Y_LABEL_FIRST+LIST_SCREEN_ORDER[4]*Y_LABEL_SPACE)
SO, my problem is when the voltage value is out of the bounds I need to highlight a particular label In red.所以,我的问题是当电压值超出范围时,我需要以红色突出显示特定的 label。 Can someone please help me how to loop these label variables to do so.有人可以帮我如何循环这些 label 变量来做到这一点。 Thanks in advance!提前致谢!
There is a method in tkinter for labals name configure()
method. tkinter 中有一个方法用于 labals 名称configure()
方法。 I m going to give u an example to understand more:我将给你一个例子来了解更多:
global x
x=0
lbl=Label(root,text=x)
lbl.pack()
for I in range [0,20] :
sleep(1)
lbl.configure(text=str(x+1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.