0

I am trying to learn the use of Class inheritance. I have read several tutorials but get stuck here, error when run is:

AttributeError: type object 'Ui_QMainWindow' has no attribute 'START'

This is just a sample code, I actually want to reference several buttons, slider and text from Ui_QMainWindow. The reason why I declare both Classes as Objects is because eventually I want to run the StopButton Class as a thread.

import sys

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import QObject

class StopButton(QObject):
    def __init__(self):
        super(StopButton, self).__init__()

    def Stop_Pressed(self):
        Ui_QMainWindow.START.setEnabled(False)

class Ui_QMainWindow(QObject):
    def __init__(self):
        super(Ui_QMainWindow, self).__init__()

    def setupUi(self, QMainWindow):
        QMainWindow.setObjectName("QMainWindow")
        QMainWindow.resize(400, 400)

        # Main Window
        self.centralwidget = QtWidgets.QWidget(QMainWindow)
        self.centralwidget.setObjectName("centralwidget")

        # STOP Button
        self.STOP = QtWidgets.QPushButton(self.centralwidget)
        self.STOP.setGeometry(QtCore.QRect(10, 10, 60, 60))
        self.STOP.setStyleSheet("QPushButton"
                                "{"
                                "background-color : red;"
                                "}"
                                "QPushButton::pressed"
                                "{"
                                "background-color : red;"
                                "}"
                                )
        self.STOP.setObjectName("STOP")
        self.STOP.setDisabled(False)
        self.STOP.clicked.connect(StopButton.Stop_Pressed)
        # self.STOP.clicked.connect(StopButton.Stop_Pressed(self))            


        # START button
        self.START = QtWidgets.QPushButton(self.centralwidget)
        self.START.setGeometry(QtCore.QRect(10, 80, 60, 60))
        self.START.setStyleSheet("QPushButton"
                                 "{"
                                 "background-color : lime;"
                                 "}"
                                 "QPushButton::pressed"
                                 "{"
                                 "background-color : lime;"
                                 "}"
                                 )
        self.START.setDisabled(False)

        QMainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(QMainWindow)

    def retranslateUi(self, QMainWindow):
        #
        _translate = QtCore.QCoreApplication.translate
        QMainWindow.setWindowTitle(_translate("QMainWindow", "Test"))
        self.STOP.setText(_translate("QMainWindow", "STOP"))
        self.START.setText(_translate("QMainWindow", "START"))

def main():
    app = QtWidgets.QApplication(sys.argv)
    QMainWindow = QtWidgets.QMainWindow()
    ui = Ui_QMainWindow()
    ui.setupUi(QMainWindow)
    QMainWindow.show()

    sys.exit(app.exec_())


main()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • What you are trying to do cannot and should not be done. To access an attribute then you must do it through the object, not through the class. On the other hand it seems that you have XY problem since according to what you say you want to call Stop_Pressed from another thread but Stop_Pressed modifies the GUI, that is to say that in the end you try to modify the GUI from another thread which is prohibited. If you want to modify the GUI from another thread then you must do it indirectly through the signals and in SO there are literally thousands of examples of that type. – eyllanesc Apr 11 '21 at 20:43
  • See https://stackoverflow.com/questions/13420931/pyqt-modify-gui-from-another-thread and https://stackoverflow.com/questions/9957195/updating-gui-elements-in-multithreaded-pyqt – eyllanesc Apr 11 '21 at 20:44
  • Thank you, as a newbie I used the wrong terminology for searching, this helps a lot and I will try again, thank you. – user14868399 Apr 12 '21 at 05:07

0 Answers0