在 PyQT GUI 中循环更新标签 [英] Loop to Update Labels in PyQT GUI
问题描述
我有一个 pyqt gui,其中包含在 init 上填充数据的标签.标签本身很容易更新:
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')
推荐答案
做到这一点的简洁方法是将所有标签的列表存储在某处,即 self.all_labels = [self.ui.mylabel_1, self.ui.mylabel_2]
然后为 self.all_labels 中的标签做 : label.setTest('foobar')
.
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')
.
您也可以使用 getattr
使您的示例工作:
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')
这篇关于在 PyQT GUI 中循环更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!