Josh Usre
Josh Usre

Reputation: 683

Loop to Update Labels in PyQT GUI

I have a pyqt gui that among other things, has labels that are populated with data on init. The labels themselves are easy to update with:

self.ui.mylabel_1.setText('foobar')
self.ui.mylabel_2.setText('foobar')

Is there any way I can loop over this in a similar way to do this as in my following pseudo:

for num in range (1,24):
   self.ui.mylabel_num.setTest('foobar')

Upvotes: 1

Views: 2057

Answers (1)

The Compiler
The Compiler

Reputation: 11939

The clean way to do this is to store a list of all your labels somewhere, i.e. self.all_labels = [self.ui.mylabel_1, self.ui.mylabel_2] and then doing for label in self.all_labels: label.setTest('foobar').

You could also use getattr to make your example work:

for num in range (1,24):
   label = getattr(self.ui, 'mylabel_{}'.format(num))
   label.setTest('foobar')

Upvotes: 1

Related Questions