PYQt5:对象没有属性

PYQt5: Object has no attribute

我是新手。我写了一个小代码,它应该允许我从一个 ui 更改为另一个。不幸的是,我收到以下错误消息:

"GUI_PYQT\main.py",第 33 行,在 init 中 self.ui.pushButton.clicked.connect(self.hello) AttributeError: 'MainWindow' 对象没有属性 'hello'"

有人可以帮助我吗?这是我的代码:


import sys

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QPushButton, QDockWidget, QToolButton
from PyQt5.QtCore import pyqtSlot

## ==> SPLASH SCREEN
from ui_splash_screen import Ui_SplashScreen

## ==> MAIN WINDOW
from ui_welcome_screen import Ui_MainWindow

## ==> HOME-SCREEN
from ui_home_screen import Ui_HomeScreen

## ==> GLOBALS
progressBarValue = 7

## WELCOME SCREEN
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.setCursor(Qt.BlankCursor)
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.ui.pushButton.clicked.connect(self.hello)


## HOME-SCREEN
class HomeScreen(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_HomeScreen()
        self.ui.setupUi(self)
        self.setCursor(Qt.BlankCursor)
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)

    def hello(self):
        self.main = Ui_HomeScreen
        self.show()


## SPLASH SCREEN
class SplashScreen(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_SplashScreen()
        self.ui.setupUi(self)
        self.setCursor(Qt.BlankCursor)

        ## UI ==> INTERFACE CODES
        ###############################################################

        ## REMOVE TITLE BAR
        self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)


        ## DROP SHADOW EFFECT
        self.shadow = QGraphicsDropShadowEffect(self)
        self.shadow.setBlurRadius(20)
        self.shadow.setXOffset(0)
        self.shadow.setYOffset(0)
        self.shadow.setColor(QColor(0, 0, 0, 0))
        self.ui.dropShadowFrame.setGraphicsEffect(self.shadow)

        ## QTIMER ==> START
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.appProgress)
        # TIMER IN MILLISECONDS
        self.timer.start(25)


        ## SHOW ==> MAIN WINDOW
        ###############################################################
        self.show()
        ## ==> END ##
        
            


    ## ==> APP FUNCTIONS
    ###################################################################

    def appProgress(self):
        global progressBarValue

        #Apply progressBarValue to the progressBar
        self.ui.progressBar.setValue(progressBarValue)
        progressBarValue = progressBarValue+1

        if progressBarValue > 100:
            # STOP TIMER
            self.timer.stop() 

            # SHOW MAIN WINDOW
            self.main = MainWindow()
            self.main.show()
            

            #CLOSE SPLASH SCREEN
            self.close()
        
        if progressBarValue >= 0:
            self.ui.label_3.setText("<strong>setting bootoptions.yo...")
        if progressBarValue >= 21:
            self.ui.label_3.setText("<strong>change screenlayout.uf...")
        if progressBarValue >= 53:
            self.ui.label_3.setText("<strong>load colors.ou")
        if progressBarValue >= 67:
            self.ui.label_3.setText("<strong>translate folder image_alternates.nd")
        if progressBarValue >= 92:
            self.ui.label_3.setText("<strong>preparing metadata.it")
        

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = SplashScreen()
    sys.exit(app.exec_())

您的 hello 函数是 HomeScreen class 的一部分,但您正在尝试连接,因此 运行 它来自 MainWindow 内部。由于 class 没有具有该名称的函数,因此会引发错误。