My main class Desktop don't return buttons of second class .Why is this happening . I was creating method add_button in main class not using second class, but part of initUI stop working because of inheritance , so i accepted resolve create additional class

class Another_desktop(QMainWindow):
    def add_button(self):
        self.btn_play=QPushButton()
        self.btn_play.setStyleSheet('background-color: blue')
        self.btn_play.setIcon(QtGui.QIcon('icons8-circled-play-120.png'))
        self.btn_play.setIconSize(QtCore.QSize(22,22))
        self.btn_play.move(10,40)
        self.btn_stop=QPushButton()
        self.btn_stop.setStyleSheet('background-color: blue')
        self.btn_stop.setIcon(QtGui.QIcon('icons8-pause-button-40.png'))
        self.btn_stop.setIconSize(QtCore.QSize(22,22))
        self.btn_stop.move(50,40)
        self.btn_next=QPushButton()
        self.btn_next.setStyleSheet('background-color: blue')
        self.btn_next.setIcon(QtGui.QIcon('icons8-back-40.png'))
        self.btn_next.setIconSize(QtCore.QSize(22,22))
        self.btn_next.move(200,40)


class Desktop(QMainWindow):
    def __init__(self):
        super().__init__()
        self.__count=0
        self.__layout=0
        self.add_button()
        self.__button_open()
        self.click()
        self.initUI()

    def initUI(self):
        exitAction = QAction(QIcon('exit.png'), '&Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(qApp.quit)
        self.statusBar()
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAction)
        self.background=self.setStyleSheet('background-color:blue')
        self.size=self.setGeometry(200,200,300,300)
        self.title=self.setWindowTitle('app')
        self.show()

    def add_button(self):
        self.add_but=Another_desktop().add_button()

1 Answers

1
eyllanesc On

If you want to add the buttons to your main class you should not create another class.

In Qt for a widget like the button to be part of the window it must be a child of the window, and to be a child of the window there are at least 2 ways: to set the widget to a layout that belongs to the window to have as a parent a element of the window, in the case of widgets like the QPushButton the parent is passed in the constructor.

On the other hand I see that you have code that does not make sense like:

self.background=self.setStyleSheet('background-color:blue')
self.size=self.setGeometry(200,200,300,300)
self.title=self.setWindowTitle('app')

the setXXX functions in Qt do not return anything since they are setters so self.background, self.size, and self.title will be None.

from PyQt5 import QtCore, QtGui, QtWidgets


class Desktop(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.__count = 0
        self.__layout = 0
        self.add_button()
        # self.__button_open()
        # self.click()
        self.initUI()

    def initUI(self):
        exitAction = QtWidgets.QAction(QtGui.QIcon("exit.png"), "&Exit", self)
        exitAction.setShortcut("Ctrl+Q")
        exitAction.setStatusTip("Exit application")
        exitAction.triggered.connect(QtWidgets.qApp.quit)
        menubar = self.menuBar()
        fileMenu = menubar.addMenu("&File")
        fileMenu.addAction(exitAction)
        self.setStyleSheet("background-color:blue")
        self.setGeometry(200, 200, 300, 300)
        self.setWindowTitle("app")
        self.show()

    def add_button(self):
        self.btn_play = QtWidgets.QPushButton(self) # <--- set parent of btn_play
        # to self
        self.btn_play.setStyleSheet("background-color: blue")
        self.btn_play.setIcon(QtGui.QIcon("icons8-circled-play-120.png"))
        self.btn_play.setIconSize(QtCore.QSize(22, 22))
        self.btn_play.move(10, 40)
        self.btn_stop = QtWidgets.QPushButton(self)
        self.btn_stop.setStyleSheet("background-color: blue")
        self.btn_stop.setIcon(QtGui.QIcon("icons8-pause-button-40.png"))
        self.btn_stop.setIconSize(QtCore.QSize(22, 22))
        self.btn_stop.move(50, 40)
        self.btn_next = QtWidgets.QPushButton(self)
        self.btn_next.setStyleSheet("background-color: blue")
        self.btn_next.setIcon(QtGui.QIcon("icons8-back-40.png"))
        self.btn_next.setIconSize(QtCore.QSize(22, 22))
        self.btn_next.move(200, 40)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Desktop()
    w.show()
    sys.exit(app.exec_())