0

I'm using Python (the latest one with PyQt5) In my screen I have 30 labels and their names are label_01 to label_30. I want to change the text of several labels based on some logic.

The labels that I want to change are kept in a list of strings (names of the labels) For instance lets assume the my logic selected these labels: label_13, label_16, label_28

The question Since the list of the labels is dynamic, how can I change the labels in loop that based on the list?

I can do this: self.label_1.setText('Command sent..') But how can I pass the label name as parameter instead of "label_1"

Thank you Eyal

Eyal Tal
  • 1
  • 2
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – quamrana May 29 '20 at 10:10
  • setattr is your friend (setattr(self, label_name, value)) – E.Serra May 29 '20 at 10:11
  • I think this is what you are looking for (https://stackoverflow.com/a/2612615/12693728) ? – mrxra May 29 '20 at 10:13
  • Use a list for your labels. Or even better: give them useful names and use a dictionary. – Klaus D. May 29 '20 at 10:14

1 Answers1

0

Try setattr which receives 3 params: the object whose attribute has to be set (can be self if used inside of class definition), the name of the attribute as a string and the new value.

Try:

# let `labels_to_change` be your dynamic list of labels you want to change and set them to be equal to new_value
labels_to_change = ['label_1','label_3']
new_value = "new_value"

for label in labels_to_change:
    setattr(getattr(self, label), "text", new_value)    
Gabio
  • 9,126
  • 3
  • 12
  • 32
  • Thank you very much Gabip In your example where is the label property like: text do i have to code labels_to_change = ['label_1.text','label_3.text'] ? Thanks! – Eyal Tal May 29 '20 at 10:39
  • @EyalTal, `labels_to_change` is just an example for your dynamic list of labels to change. Just use the `for` loop and replace `labels_to_change` with your actual list of labels to be changed. – Gabio May 29 '20 at 10:41
  • I understand that, but where is the place that I have to put the attribute that i need to change for instance the .text : object.text = 'xxx' – Eyal Tal May 29 '20 at 10:51
  • @EyalTal it should be the second param of `setattr` function. In my solution I assumed that you want to iterate over dynamic list of attributes and set a new value for them so in my post, `label` equals to the attribute name you want to set. If you just want to set it once, use `setattr(object_name, attribute_name, new_value)` – Gabio May 29 '20 at 10:56
  • That's the code but it is not change the label text labels_to_change = ['label_1.text', 'label_2.text'] new_value = "new_value" for label in labels_to_change: setattr(self, label, new_value) what is wrong here? – Eyal Tal May 29 '20 at 11:01
  • The first line is working but not the second. self.label_1.setText('Command sent,') setattr(self, 'label_2.setText', 'new_value') – Eyal Tal May 29 '20 at 11:13
  • Ok, you had to mention that you want to update an attribute inside another attribute. I've updated the answer. Try now please... – Gabio May 29 '20 at 11:16
  • Gabi - still nothing changed. 'code' def docmd(self): labels_to_change = ['label_1', 'label_2'] new_value = "new_value" for label in labels_to_change: setattr(getattr(self, label), "text", new_value) – Eyal Tal May 29 '20 at 11:33
  • Could you please edit your question with your code? – Gabio May 29 '20 at 12:24
  • The problematic section is : def startBackup(self): – Eyal Tal May 29 '20 at 14:30
  • The code run with no issue but the labels are not changed. – Eyal Tal May 29 '20 at 15:52