#Changing variable names
1 messages · Page 1 of 1 (latest)
What do you mean, can't change variable names? In Python there are just names that are bound to values. How that behaves depends on whether the value type is a mutable and scope considerations.
What are you trying to accomplish?
I have several label widgets in a frame. I receive data and depending on the data want to update only one widget.
example - "label_BTN" "label_SPN" "label_B1N" and "label_FTN" widgets
receive data "BTNdeft12"
and want to update the "label_BTN" text only with "deft12"
what type of object are your labels? If your using the Adafruit_CircuitPython_Display_Text library Labels you can change the text showing on those with the text property. Like my_label.text = "something new"
The unsafe hack would be
label = globals().get(f"label_{data[:3]}")
if label:
label.configure(text=data[3:])
label.update()
But you'd want to keep a dict of labels by name, instead of digging in the complete global scope with tainted data from the network.
And you'd probably want to check out foamguy's comment.
That's what I'm doing. just modifying the text property in the label widget. I'm new to python code and I can just update all label.text properties every time i receive data, but want to have object type of flow.
There are about 16 widgets to display and i can just get by with updating the ones that data hasn't changed, but wanted to pass a different label name to a def to do the update.