我已经寻找这个问题很长时间了,我仍然不知道它是否可能。我想使用一个变量来控制一个小工具。例如,我想这样做:
self.ui.label1.setText('hello') 但是使用一个变量,比如字符串,假设:
string = 'label1'
self.ui.string.setText('hello') #this obviously doesn't work. ui doesn't recognize a string object.这个问题有什么解决方案吗?谢谢大家!
发布于 2020-04-30 21:25:30
我想有很多方法可以实现我所理解的你想要的东西。这里有两个:
在字典中存储标签的
labels = {
'label1': self.ui.label1,
'label2': self.ui.label2,
...
}
...
label = 'label1'
labels[label].setText('hello')使用getattr()的
...
label = 'label1'
getattr(self.ui, label).setText('hello')仅当标注为self.ui的属性时,此方法才有效
https://stackoverflow.com/questions/61523895
复制相似问题