PyQt5循环遍历类变量并更改状态/设置值

huangapple go评论96阅读模式
英文:

Pyqt5 loop over class variables and change state/set values

问题

我有一个带有一定数量的类变量的类。这些变量是不同类型的,例如 QLineEdit/QCheckbox 等等。如何循环遍历类变量以设置变量值如下:

  1. obj = FindObj()
  2. value = ['100', 'yes', 'False']
  3. i=0
  4. for variable in obj:
  5. if variable.__class__() == 'QLineEdit': # 不知道是否正确
  6. variable.setText(value[i])
  7. i=i+1
  8. elif variable.__class__() == 'QCheckBox':
  9. variable.setChecked(value[i])
  10. i=i+1
  11. import sys
  12. from PyQt5.QtWidgets import QApplication, QWidget
  13. from PyQt5.QtGui import QIcon
  14. class FindObj():
  15. def __init__(self):
  16. super().__init__()
  17. self.l1 = QLineEdit()
  18. self.l2 = QLineEdit()
  19. self.l3 = QCheckBox()
  20. if __name__ == '__main__':
  21. app = QApplication(sys.argv)
  22. ex = FindObj()
  23. sys.exit(app.exec_())
英文:

I have a class with certain number of class variable. Variables are of different types e.g. QLineEdit/QCheckbox....etc. How can I loop over class variable to set a variable value as below:

  1. obj = FindObj()
  2. value = ['100', 'yes', 'False']
  3. i=0
  4. for variable in obj:
  5. if variable.__class__() == 'QLineEdit': # Don't know if it's right
  6. variable.setText(value[i])
  7. i=i+1
  8. elif variable.__class__() == 'QCheckBox':
  9. variable.setChecked(value[i])
  10. i=i+1
  11. import sys
  12. from PyQt5.QtWidgets import QApplication, QWidget
  13. from PyQt5.QtGui import QIcon
  14. class FindObj():
  15. def __init__(self):
  16. super().__init__()
  17. self.l1 = QLineEdit()
  18. self.l2 = QLineEdit()
  19. self.l3 = QCheckBox()
  20. if __name__ == '__main__':
  21. app = QApplication(sys.argv)
  22. ex = FindObj()
  23. sys.exit(app.exec_())

答案1

得分: 1

如果您也将变量放入一个列表中,您可以循环遍历这两个列表:

  1. class FindObj():
  2. def __init__(self):
  3. self.l1 = QLineEdit()
  4. self.l2 = QLineEdit()
  5. self.l3 = QCheckBox()
  6. self.variables = [self.l1, self.l2, self.l3]

现在您可以循环遍历它们:

  1. obj = FindObj()
  2. values = ['100', 'yes', 'False']
  3. for variable, value in zip(obj.variables, values):
  4. if variable.__class__.__name__ == 'QLineEdit':
  5. variable.setText(value)
  6. elif variable.__class__.__name__ == 'QCheckBox':
  7. variable.setChecked(value)
英文:

If you put your variables in a list as well, you can loop over both lists:

  1. class FindObj():
  2. def __init__(self):
  3. self.l1 = QLineEdit()
  4. self.l2 = QLineEdit()
  5. self.l3 = QCheckBox()
  6. self.variables = [self.l1, self.l2, self.l3]

Now you can loop over them:

  1. obj = FindObj()
  2. values = ['100', 'yes', 'False']
  3. for variable, value in zip(obj.variables, values):
  4. if variable.__class__.__name__ == 'QLineEdit':
  5. variable.setText(value)
  6. elif variable.__class__.__name__ == 'QCheckBox':
  7. variable.setChecked(value)

huangapple
  • 本文由 发表于 2020年1月6日 02:20:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/59602892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定