问题描述
我正在尝试使用 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 (将#修改为@)