0

I'm trying to update QLabel() with setText.

    from PyQt5 import QtCore, QtGui, QtWidgets

    import RPi.GPIO as GPIO
    
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BOARD)
    # input
    GPIO.setup(37, GPIO.IN)  # sensor jolo (camera)
    # output
    GPIO.setup(8, GPIO.OUT, initial=True)  # pump

    class Ui_Form(object):
        def setupUi(self, Form):
            Form.setObjectName("Form")
            Form.resize(400, 300)
            self.label = QtWidgets.QLabel(Form)
            self.label.setGeometry(QtCore.QRect(90, 30, 47, 13))
            self.label.setObjectName("label")
            self.lineEdit = QtWidgets.QLineEdit(Form)
            self.lineEdit.setGeometry(QtCore.QRect(30, 70, 113, 20))
            self.lineEdit.setObjectName("lineEdit")
            self.lineEdit_2 = QtWidgets.QLineEdit(Form)
            self.lineEdit_2.setGeometry(QtCore.QRect(30, 110, 113, 20))
            self.lineEdit_2.setObjectName("lineEdit_2")
            self.pushButton = QtWidgets.QPushButton(Form)
            self.pushButton.setGeometry(QtCore.QRect(220, 120, 75, 23))
            self.pushButton.setObjectName("pushButton")
            self.pushButton.clicked.connect(self.set)
            self.retranslateUi(Form)
            QtCore.QMetaObject.connectSlotsByName(Form)

        def sens(self):
            if GPIO.input(37):
                GPIO.output (8, False)
                self.label.setText('1234')
            else:
                GPIO.output (8, True)
                pass

        GPIO.add_event_detect(37, GPIO.BOTH, callback=sens, bouncetime=10)

        def set(self):
            self.label.setText('1234')

        def retranslateUi(self, Form):
            _translate = QtCore.QCoreApplication.translate
            Form.setWindowTitle(_translate("Form", "Form"))
            self.label.setText(_translate("Form", "TextLabel"))
            self.pushButton.setText(_translate("Form", "PushButton"))


    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        Form = QtWidgets.QWidget()
        ui = Ui_Form()
        ui.setupUi(Form)
        Form.show()
        sys.exit(app.exec_())

The set function, which is activated with a pushButton, is executed correctly. But The sens function that is activated with add_event_detect has an error. Error text : self.label.setText('1234') /n AttributeError: 'int' object has no attribute 'label'

aryan261
  • 1
  • 1
  • 1) move `GPIO.add_event_detect(37, GPIO.BOTH, callback=sens, bouncetime=10)` inside some method of Ui_Form class. change to `GPIO.add_event_detect(37, GPIO.BOTH, callback=self.sens, bouncetime=10)` and use signals – eyllanesc Apr 15 '21 at 04:57
  • In this case, the whole program will encounter an error – aryan261 Apr 15 '21 at 06:23

0 Answers0