Value from class to class
-
wrote on 10 May 2021, 10:51 last edited by sashup 5 Oct 2021, 10:55
How to pass value
mean
from class to class? I try to do it with the help ofclass parametrs
but It doesn't work.class ClassesPage1(QtWidgets.QWizardPage): ... def check(self): a = 0 for checkBox in self.listCheckBox: if checkBox.isChecked(): a += 1 mean = a / 4 if mean == 1: self.label_1.setText('Avarage: 1') elif mean == 0: self.label_1.setText('Avarage: 0') else: self.label_1.setText('Avarage: ' + str(mean)) def nextId(self): return Wizard.class4 class parametrs: par = ClassesPage1() print par.mean
-
How to pass value
mean
from class to class? I try to do it with the help ofclass parametrs
but It doesn't work.class ClassesPage1(QtWidgets.QWizardPage): ... def check(self): a = 0 for checkBox in self.listCheckBox: if checkBox.isChecked(): a += 1 mean = a / 4 if mean == 1: self.label_1.setText('Avarage: 1') elif mean == 0: self.label_1.setText('Avarage: 0') else: self.label_1.setText('Avarage: ' + str(mean)) def nextId(self): return Wizard.class4 class parametrs: par = ClassesPage1() print par.mean
@sashup "mean" is local variable and only exists inside def check(self). It needs to be class member (self.mean) if you want to use it outside of def check(self).
-
wrote on 10 May 2021, 11:49 last edited by sashup 5 Oct 2021, 11:50
@jsulm
somethig like?def check(self): a = 0 for checkBox in self.listCheckBox: if checkBox.isChecked(): a += 1 self.mean = a / 4 if self.mean == 1: self.label_1.setText('Avarage: 1') elif self.mean == 0: self.label_1.setText('Avarage: 0') else: self.label_1.setText('Avarage:' + str(self.mean))
-
@jsulm
somethig like?def check(self): a = 0 for checkBox in self.listCheckBox: if checkBox.isChecked(): a += 1 self.mean = a / 4 if self.mean == 1: self.label_1.setText('Avarage: 1') elif self.mean == 0: self.label_1.setText('Avarage: 0') else: self.label_1.setText('Avarage:' + str(self.mean))
-
-
@jsulm
Then in the second class I write:class parametrs: def __init__(self, *args, **kwargs): super(parametrs, self).__init__(*args, **kwargs) print(self.mean)
But It doesn't work
Lifetime Qt Championwrote on 10 May 2021, 12:00 last edited by jsulm 5 Oct 2021, 12:01@sashup said in Value from class to class:
But It doesn't work
Of course not. This code does not make sense.
"mean" is member of the class ClassesPage1, right? So, how should self.mean work in a completely different class?
Why did you change parametrs class? Before it wasclass parametrs: par = ClassesPage1() print par.mean
and this version should work now.
Also, keep in mind that currently self.mean only exists if check() was executed!
You should really read a book about Python as you're asking basic questions completely unrelated to Qt.
-
@sashup said in Value from class to class:
But It doesn't work
Of course not. This code does not make sense.
"mean" is member of the class ClassesPage1, right? So, how should self.mean work in a completely different class?
Why did you change parametrs class? Before it wasclass parametrs: par = ClassesPage1() print par.mean
and this version should work now.
Also, keep in mind that currently self.mean only exists if check() was executed!
You should really read a book about Python as you're asking basic questions completely unrelated to Qt.
-
@sashup Then please show your current code and write what exactly happens instead of writing that it "doesn't work"...
-
@sashup Then please show your current code and write what exactly happens instead of writing that it "doesn't work"...
wrote on 10 May 2021, 12:08 last edited by@jsulm
print par.mean
doesn't give any valuesclass ClassesPage1(QtWidgets.QWizardPage): def __init__(self, *args, **kwargs): super(ClassesPage1, self).__init__(*args, **kwargs) font = QtGui.QFont() font.setFamily("Sitka") font.setPointSize(14) self.setFont(font) self.label_3 = QLabel("...", self) self.label_3.setAlignment(Qt.AlignCenter) self.label_4 = QLabel("...", self) self.label_4.setAlignment(Qt.AlignBottom) self.checkBox_1 = QtWidgets.QCheckBox('...') self.checkBox_2 = QtWidgets.QCheckBox('...') self.checkBox_3 = QtWidgets.QCheckBox('...') self.checkBox_4 = QtWidgets.QCheckBox('...') self.label_1 = QtWidgets.QLabel('Avarage: 0') #self.label_1.setAlignment(Qt.AlignLeft) self.layout = QtWidgets.QVBoxLayout() self.layout.addWidget(self.label_3) self.layout.addWidget(self.label_4) 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.layout.addWidget(self.label_1) self.setLayout(self.layout) self.performance() self.listCheckBox = [self.checkBox_1, self.checkBox_2, self.checkBox_3, self.checkBox_4] def performance(self): 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): a = 0 for checkBox in self.listCheckBox: if checkBox.isChecked(): a += 1 self.mean = a / 4 if self.mean == 1: self.label_1.setText('Avarage: 1') elif self.mean == 0: self.label_1.setText('Avarage: 0') else: self.label_1.setText('Avarage:' + str(self.mean))xtId(self): return Wizard.class4 class parametrs: par = ClassesPage1() print par.mean if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) w = ClassesPage1() w.show() sys.exit(app.exec_())
-
@jsulm
print par.mean
doesn't give any valuesclass ClassesPage1(QtWidgets.QWizardPage): def __init__(self, *args, **kwargs): super(ClassesPage1, self).__init__(*args, **kwargs) font = QtGui.QFont() font.setFamily("Sitka") font.setPointSize(14) self.setFont(font) self.label_3 = QLabel("...", self) self.label_3.setAlignment(Qt.AlignCenter) self.label_4 = QLabel("...", self) self.label_4.setAlignment(Qt.AlignBottom) self.checkBox_1 = QtWidgets.QCheckBox('...') self.checkBox_2 = QtWidgets.QCheckBox('...') self.checkBox_3 = QtWidgets.QCheckBox('...') self.checkBox_4 = QtWidgets.QCheckBox('...') self.label_1 = QtWidgets.QLabel('Avarage: 0') #self.label_1.setAlignment(Qt.AlignLeft) self.layout = QtWidgets.QVBoxLayout() self.layout.addWidget(self.label_3) self.layout.addWidget(self.label_4) 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.layout.addWidget(self.label_1) self.setLayout(self.layout) self.performance() self.listCheckBox = [self.checkBox_1, self.checkBox_2, self.checkBox_3, self.checkBox_4] def performance(self): 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): a = 0 for checkBox in self.listCheckBox: if checkBox.isChecked(): a += 1 self.mean = a / 4 if self.mean == 1: self.label_1.setText('Avarage: 1') elif self.mean == 0: self.label_1.setText('Avarage: 0') else: self.label_1.setText('Avarage:' + str(self.mean))xtId(self): return Wizard.class4 class parametrs: par = ClassesPage1() print par.mean if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) w = ClassesPage1() w.show() sys.exit(app.exec_())
@sashup I already wrote that self.mean is only set if def check(self) was executed at least once. Do you realise that?
This should work:class parametrs: par = ClassesPage1() par.check() print par.mean
-
@sashup I already wrote that self.mean is only set if def check(self) was executed at least once. Do you realise that?
This should work:class parametrs: par = ClassesPage1() par.check() print par.mean
wrote on 10 May 2021, 12:42 last edited by@jsulm It was solved like
import sys from PyQt5 import QtCore, QtGui, QtWidgets class ClassesPage1(QtWidgets.QWizardPage): def __init__(self, parent=None): super(ClassesPage1, self).__init__() self.setTitle("...") self.setSubTitle("...") self.label_1 = QtWidgets.QLabel('label_1') self.checkBox_1 = QtWidgets.QCheckBox('cb1 ...') self.checkBox_2 = QtWidgets.QCheckBox('cb2 ...') self.checkBox_3 = QtWidgets.QCheckBox('cb3 ...') self.checkBox_4 = QtWidgets.QCheckBox('cb4 ...') 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.layout.addWidget(self.label_1) 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 self.parent = parent self.mean = 0 def check(self): self.a = 0 for checkBox in self.listCheckBox: if checkBox.isChecked(): self.a += 1 self.mean = self.a / 4 self.label_1.setText(f'Avarage: {self.mean}') self.parent._print() def nextId(self): return Wizard.class4 class Parametrs: def __init__(self): super().__init__() self.par = ClassesPage1(self) self.par.show() print(f'__init__: par.mean = {self.par.mean}') def _print(self): print(f'par.mean = {self.par.mean:.2f}') if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) w = Parametrs() sys.exit(app.exec_())
1/11