为了账号安全,请及时绑定邮箱和手机立即绑定

如何解决此错误“对象没有属性”

如何解决此错误“对象没有属性”

摇曳的蔷薇 2022-07-26 10:34:27
错误:self.START.clicked.connect(self.StartClicked)AttributeError:“代码”对象没有属性“StartClicked”这是我的代码。我不知道我在这段代码中哪里错了,请你检查一下并纠正我哪里错了。当我尝试在没有 GUI 的情况下运行代码时,它工作正常,但使用 QT 时,它给了我这个错误。import sysimport cv2import datetimefrom PyQt5.QtWidgets import QApplication ,QDialogfrom PyQt5.uic import loadUifrom PyQt5.QtGui import QImage,QPixmapfrom PyQt5.QtCore import pyqtSlotclass code(QDialog):   def __init__(self):        super(code,self).__init__()        loadUi('letst.ui',self)        self.logic =0        self.START.clicked.connect(self.StartClicked)        self.STOP.clicked.connect(self.StopClicked)@pyqtSlot()def StartClicked(self):        self.logic=1        fourcc = cv2.VideoWriter_fourcc(*'XVID')        date=datetime.datetime.now()        cap = cv2.VideoCapture(1)        out= cv2.VideoWriter('E:/project/videos/rec_%s%s%s%s%s%s.mp4' %(date.year,date.month,date.day,date.hour,date.minute,date.second),fourcc,20.0,(640,480))        while (cap.isOpened()):                ret,frame = cap.read()                if ret == True:                        self.displayImage(frame,1)                        cv2.waitKey()                        if  (self.logic==1):                                out.write(frame)                        if (self.logic==0):                                break                else:                    print('return not found')                cap.release()                out.release()                cv2.destroyAllWindows()def StopClicked(self):    self.logic=0def displayImage(self, img,window=1):    qformat = QImage.Format_Indexed8    if len(img.shape)==3:        if (img.shape[2])==4:            qformat=QImage.Format_RGBA888        else:            qformat=QImage.Format_RGB888    img =QImage(img, img.shape[1],img.shape[0],qformat)    img =img.rgbSwapped()    self.imglabel.setPixmap(QPixmap.fromImage(img))    self.imgLabel.setAlignment(QtCore.Qt.AlignHCenter | Qtcore.Qt.AlignVCenter)
查看完整描述

1 回答

?
POPMUISE

TA贡献1765条经验 获得超5个赞

以下函数应该在类中:


def StartClicked(self):

def StopClicked(self):

def displayImage

请更正缩进并尝试。


查看完整回答
反对 回复 2022-07-26
  • 1 回答
  • 0 关注
  • 272 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号