AttributeError: 'MyMainWindow' object has no attribute 'pushButton'

14,269

In your code there are 3 errors:

  1. You must call the setupUI function first since the button is created here and then connect that button to the slot.
  2. When the button is connected to the slot you just have to pass the slot name without parenthesis.
  3. Change statusbar() to statusBar().

Code:

# ...
class MyMainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        qApp.installEventFilter(self)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.clickButton)
        self.show()
# ...
    def clickButton(self):
        sender = self.sender()
        self.statusBar().showMessage(sender.text() + ' was pressed')
# ...
Share:
14,269
Michael
Author by

Michael

"Programming is like sex. One mistake and you have to support it for the rest of your life." (Michael Sinz)

Updated on June 05, 2022

Comments

  • Michael
    Michael almost 2 years

    Trying to proceed click button event in my code but got some issue.

    AttributeError: 'MyMainWindow' object has no attribute 'pushButton'
    

    Seems, like clicked event can`t find pushbutton from my subclass. Probably i did some mistakes in syntax, so really waiting for your help guys. Sorry if the question very banal, pyQt5 is simply new for me, trying to figure out in all of this.

    Here is my files.

    ui_main.py

        # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file '1.ui'
    #
    # Created by: PyQt5 UI code generator 5.7.1
    #
    # WARNING! All changes made in this file will be lost!
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(349, 131)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
            self.gridLayout.setObjectName("gridLayout")
            self.formLayout = QtWidgets.QFormLayout()
            self.formLayout.setObjectName("formLayout")
            self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
            self.lineEdit.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignTop)
            self.lineEdit.setObjectName("lineEdit")
            self.formLayout.setWidget(1, QtWidgets.QFormLayout.SpanningRole, self.lineEdit)
            self.label = QtWidgets.QLabel(self.centralwidget)
            self.label.setAlignment(QtCore.Qt.AlignCenter)
            self.label.setObjectName("label")
            self.formLayout.setWidget(0, QtWidgets.QFormLayout.SpanningRole, self.label)
            self.gridLayout.addLayout(self.formLayout, 0, 0, 1, 1)
            self.horizontalLayout = QtWidgets.QHBoxLayout()
            self.horizontalLayout.setObjectName("horizontalLayout")
            self.pushButton = QtWidgets.QPushButton(self.centralwidget)
            self.pushButton.setObjectName("pushButton")
            self.horizontalLayout.addWidget(self.pushButton)
            self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
            self.pushButton_2.setObjectName("pushButton_2")
            self.horizontalLayout.addWidget(self.pushButton_2)
            self.gridLayout.addLayout(self.horizontalLayout, 1, 0, 1, 1)
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 349, 21))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.label.setText(_translate("MainWindow", "TextLabel"))
            self.pushButton.setText(_translate("MainWindow", "PushButton"))
            self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())
    

    py_main.py

    import sys
    from PyQt5.QtWidgets import QApplication, QMainWindow, qApp
    from PyQt5.QtCore import Qt, QEvent, QObject
    from ui_main import Ui_MainWindow
    
    
    class MyMainWindow(QMainWindow, Ui_MainWindow):
        def __init__(self, parent=None):
            super(MyMainWindow, self).__init__(parent)
            qApp.installEventFilter(self)
            self.pushButton.clicked.connect(self.clickButton()) #here is where the issue is occurs
            self.show()
            self.setupUi(self)
    
        def eventFilter(self, obj, event):
            if event.type() == QEvent.KeyPress:
                if event.key() == Qt.Key_Escape:
                    self.close()
            return super(MyMainWindow, self).eventFilter(obj, event)
    
        def clickButton(self):
            sender = self.sender()
            self.statusbar().showMessage(sender.text() + ' was pressed')
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        win = MyMainWindow()
        test = TestClass()
        sys.exit(app.exec_())
    

    Waiting forward for your help!

  • Michael
    Michael about 7 years
    what should if the form closes after pressing pushButton?
  • eyllanesc
    eyllanesc about 7 years
    I do not understand you, explain yourself better.