sum of the click on button
-
How can I count sum of the click on button? For exemple, now I have only 1 1 1 if buttons was clicked 3 times. But I need 1+1+1
class ClassesPage1(QtWidgets.QWizardPage): def __init__(self, *args, **kwargs): super(ClassesPage1, self).__init__(*args, **kwargs) super(ClassesPage1, self).__init__(*args, **kwargs) self.setTitle("...") self.setSubTitle("...") self.checkBox_1 = QtWidgets.QCheckBox('...') self.checkBox_2 = QtWidgets.QCheckBox('...') self.checkBox_3 = QtWidgets.QCheckBox('...') self.checkBox_4 = QtWidgets.QCheckBox('...') self.layout = QtWidgets.QVBoxLayout() self.layout.addWidget(self.checkBox_1) self.layout.addWidget(self.checkBox_2) self.layout.addWidget(self.checkBox_3) self.layout.addWidget(self.checkBox_4) self.setLayout(self.layout) self.checkBox_1.stateChanged.connect(self.check) self.checkBox_2.stateChanged.connect(self.check) self.checkBox_3.stateChanged.connect(self.check) self.checkBox_4.stateChanged.connect(self.check) def check(self, state): a = 0 if state == Qt.Checked: a = a + 1 print(a) def nextId(self): return Wizard.class4
-
How can I count sum of the click on button? For exemple, now I have only 1 1 1 if buttons was clicked 3 times. But I need 1+1+1
class ClassesPage1(QtWidgets.QWizardPage): def __init__(self, *args, **kwargs): super(ClassesPage1, self).__init__(*args, **kwargs) super(ClassesPage1, self).__init__(*args, **kwargs) self.setTitle("...") self.setSubTitle("...") self.checkBox_1 = QtWidgets.QCheckBox('...') self.checkBox_2 = QtWidgets.QCheckBox('...') self.checkBox_3 = QtWidgets.QCheckBox('...') self.checkBox_4 = QtWidgets.QCheckBox('...') self.layout = QtWidgets.QVBoxLayout() self.layout.addWidget(self.checkBox_1) self.layout.addWidget(self.checkBox_2) self.layout.addWidget(self.checkBox_3) self.layout.addWidget(self.checkBox_4) self.setLayout(self.layout) self.checkBox_1.stateChanged.connect(self.check) self.checkBox_2.stateChanged.connect(self.check) self.checkBox_3.stateChanged.connect(self.check) self.checkBox_4.stateChanged.connect(self.check) def check(self, state): a = 0 if state == Qt.Checked: a = a + 1 print(a) def nextId(self): return Wizard.class4
-
I solved it like:
import sys from PyQt5 import QtCore, QtGui, QtWidgets class ClassesPage1(QtWidgets.QWizardPage): def __init__(self, *args, **kwargs): super(ClassesPage1, self).__init__(*args, **kwargs) # super(ClassesPage1, self).__init__(*args, **kwargs) self.setTitle("...") self.setSubTitle("...") self.checkBox_1 = QtWidgets.QCheckBox('...') self.checkBox_2 = QtWidgets.QCheckBox('...') self.checkBox_3 = QtWidgets.QCheckBox('...') self.checkBox_4 = QtWidgets.QCheckBox('...') self.layout = QtWidgets.QVBoxLayout() self.layout.addWidget(self.checkBox_1) self.layout.addWidget(self.checkBox_2) self.layout.addWidget(self.checkBox_3) self.layout.addWidget(self.checkBox_4) self.setLayout(self.layout) self.checkBox_1.stateChanged.connect(self.check) self.checkBox_2.stateChanged.connect(self.check) self.checkBox_3.stateChanged.connect(self.check) self.checkBox_4.stateChanged.connect(self.check) self.listCheckBox = [self.checkBox_1, self.checkBox_2, self.checkBox_3, self.checkBox_4] self.a = 0 def check(self, state): self.a = 0 # if state == Qt.Checked: # a = a + 1 for checkBox in self.listCheckBox: if checkBox.isChecked(): self.a += 1 print(self.a) def nextId(self): return Wizard.class4 if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) w = ClassesPage1() w.show() sys.exit(app.exec_())
-
I solved it like:
import sys from PyQt5 import QtCore, QtGui, QtWidgets class ClassesPage1(QtWidgets.QWizardPage): def __init__(self, *args, **kwargs): super(ClassesPage1, self).__init__(*args, **kwargs) # super(ClassesPage1, self).__init__(*args, **kwargs) self.setTitle("...") self.setSubTitle("...") self.checkBox_1 = QtWidgets.QCheckBox('...') self.checkBox_2 = QtWidgets.QCheckBox('...') self.checkBox_3 = QtWidgets.QCheckBox('...') self.checkBox_4 = QtWidgets.QCheckBox('...') self.layout = QtWidgets.QVBoxLayout() self.layout.addWidget(self.checkBox_1) self.layout.addWidget(self.checkBox_2) self.layout.addWidget(self.checkBox_3) self.layout.addWidget(self.checkBox_4) self.setLayout(self.layout) self.checkBox_1.stateChanged.connect(self.check) self.checkBox_2.stateChanged.connect(self.check) self.checkBox_3.stateChanged.connect(self.check) self.checkBox_4.stateChanged.connect(self.check) self.listCheckBox = [self.checkBox_1, self.checkBox_2, self.checkBox_3, self.checkBox_4] self.a = 0 def check(self, state): self.a = 0 # if state == Qt.Checked: # a = a + 1 for checkBox in self.listCheckBox: if checkBox.isChecked(): self.a += 1 print(self.a) def nextId(self): return Wizard.class4 if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) w = ClassesPage1() w.show() sys.exit(app.exec_())
-
@sashup
Your changed code counts something very different. Now there is no reason to haveself.a
as a member variable, it can be a local variable.