#Changing variable names

1 messages · Page 1 of 1 (latest)

naive wedge
#

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?

keen canyon
#

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"

somber girder
naive wedge
#

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.

keen canyon