PyQt5 setText 与 add_event_detect

问题描述

我正在尝试使用 setText 更新 QLabel()。

    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,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_())

通过按钮激活的设置功能正确执行。 但是用 add_event_detect 激活的 sens 函数错误错误文本:self.label.setText('1234') /n AttributeError: 'int' 对象没有属性 'label'

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...