I'm trying to loop over all the checkboxes in my PyQt5 gui.
for i in range(2):
self.checkbox+i
This gives me the error 'Window has no checkbox attribute'. Window.checkbox doesn't indeed exist. It needs a number.
I've tried multiple things like:
for N in range(2):
obj = self.checkbox+N
print(obj.text())
Why does my for loop fail or better yet, how do I get my for loop running? Any suggestions are welcome.
Greets, Michel
# working example
#!/usr/bin/env python3
from PyQt5.QtWidgets import *
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
layout = QGridLayout()
self.setLayout(layout)
self.checkbox0 = QCheckBox("checkMe0")
self.checkbox0.toggled.connect(self.checkbox_toggled)
layout.addWidget(self.checkbox0, 0, 0)
self.checkbox1 = QCheckBox("checkMe1")
self.checkbox1.toggled.connect(self.checkbox_toggled)
layout.addWidget(self.checkbox1, 1, 0)
def checkbox_toggled(self):
print(self.checkbox0.text())
app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())