2

How can I link checkbox to the variable under pushbutton? The checkbox will return either 0 or 1 for the variable F which will affect the calculation output.

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 394)
        self.spinBox = QtWidgets.QSpinBox(Dialog)
        self.spinBox.setGeometry(QtCore.QRect(60, 120, 48, 24))
        self.spinBox.setObjectName("spinBox")
        self.spinBox.setRange(1, 200)
        self.spinBox.setValue(0)
        self.checkBox = QtWidgets.QCheckBox(Dialog)
        self.checkBox.setGeometry(QtCore.QRect(60, 160, 87, 20))
        self.checkBox.setObjectName("checkBox")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(50, 190, 113, 32))
        self.pushButton.setObjectName("pushButton")
        self.lcdNumber = QtWidgets.QLCDNumber(Dialog)
        self.lcdNumber.setGeometry(QtCore.QRect(90, 230, 113, 32))
        self.lcdNumber.setObjectName("lcdNumber")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        self.pushButton.clicked.connect(self.pushButton_handler)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.checkBox.setText(_translate("Dialog", "CheckBox"))
        self.pushButton.setText(_translate("Dialog", "PushButton"))
    def pushButton_handler(self, state):      
        A1 = self.spinBox.value()

        F=  ???  #F to be equal to 0 if check box checked or 1 if
                              check box unchecked. 
        i=A1*F
        app.processEvents()' 
        self.lcdNumber.display(i)
1

1 Answer 1

1

QCheckBox::stateChanged(int state)

This signal is emitted whenever the checkbox's state changes, i.e., whenever the user checks or unchecks it.

state contains the checkbox's new Qt::CheckState.

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 394)
        self.spinBox = QtWidgets.QSpinBox(Dialog)
        self.spinBox.setGeometry(QtCore.QRect(60, 120, 48, 24))
        self.spinBox.setObjectName("spinBox")
        self.spinBox.setRange(1, 200)
        self.spinBox.setValue(0)
        
        self.checkBox = QtWidgets.QCheckBox(Dialog)
        self.checkBox.setGeometry(QtCore.QRect(60, 160, 87, 20))
        self.checkBox.setObjectName("checkBox")
        
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(50, 190, 113, 32))
        self.pushButton.setObjectName("pushButton")
        
        self.lcdNumber = QtWidgets.QLCDNumber(Dialog)
        self.lcdNumber.setGeometry(QtCore.QRect(90, 230, 113, 32))
        self.lcdNumber.setObjectName("lcdNumber")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        self.pushButton.clicked.connect(self.pushButton_handler)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.checkBox.setText(_translate("Dialog", "CheckBox"))
        self.pushButton.setText(_translate("Dialog", "PushButton"))


class Example(QtWidgets.QDialog, Ui_Dialog):                        # +
    def __init__(self):
        super(Example, self).__init__()
        self.setupUi(self)   
        
        self.checkBox.stateChanged.connect(self.selectCheckBox)     # +++
        self.F = 1                                                  # +
        
    def pushButton_handler(self, state):      
        A1 = self.spinBox.value()
#        F =  #???  #F to be equal to 0 if check box checked or 1 if check box unchecked. 

        i = A1 * self.F
        
# ?       app.processEvents()' 
        self.lcdNumber.display(i) 

    def selectCheckBox(self, toggle):                                # +++   
        if toggle == QtCore.Qt.Checked:
            self.F = 0
        else:
            self.F = 1
        

if __name__ == "__main__":
    app = QApplication(sys.argv)
    default_font = QFont()
    default_font.setPointSize(15)
    app.setFont(default_font)
    w = Example()
    w.resize(220, 300)
    w.show()
    sys.exit(app.exec_())

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Oh, you did it as a method then you add it to the formula from the method. Thank you for the illustrated example. S. Nick
@MichaleMackdonalde Never change a module created in QT Designer !!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.