Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Pyqt: AttributeError: 'Object' has not attribute 'someFunction'



  • Hey there.

    I am trying to run this example:
    @import sys
    from PyQt4 import QtGui, QtCore

    class Window(QtGui.QWidget):

    def __init__(self):
        super(Window, self).__init__()       
        self.initUI()
    
    def initUI(self):
        self.setWindowTitle('Who is online?')
        self.setGeometry(50, 50, 730, 326)
    
        self.labelF = QtGui.QLabel(self)
        self.labelF.setGeometry(QtCore.QRect(395, 50, 111, 31))
        self.labelF.setFrameShape(QtGui.QFrame.StyledPanel)
        self.labelF.setAlignment(QtCore.Qt.AlignCenter)
        self.labelF.setText('')
    
        self.pushButton = QtGui.QPushButton("Quit", self)
        self.pushButton.setGeometry(QtCore.QRect(620, 290, 101, 27))
        self.pushButton.clicked.connect(QtCore.QCoreApplication.instance().quit)
    
        self.buttonA = QtGui.QPushButton("A", self)
        self.buttonA.setGeometry(QtCore.QRect(110, 290, 101, 27))
        self.buttonA.clicked.connect(self.setA)
    
        self.show()
    

    def setA(self):
    self.labelF.setText('InstructorA')

    def setD(self):
    self.labelF.setText('InstructorB')

    def main():
    app = QtGui.QApplication(sys.argv)
    ex = Window()
    sys.exit(app.exec_())

    if name == 'main':
    main()
    @

    The button 'buttonA' is causing an error with the function 'setA'. The error message says:
    Attribute Error "'Window' has no attribute 'setA'"

    I compared my code to other examples that use a button and can't figure out how my code differs from those examples... Any help would be appreciated. Thanks in advance!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Might be a silly question but did you indent your source correctly ? The code you posted shows that setA is not part of Window since it's at the same level as e.g. main


Log in to reply