-1

i'm trying to do a simple GUI for a python script that convert some text into a specific format but buttons doesn't show up in the window.

I first create the button class

class Button(QPushButton):
    def __init__(self, btn_name=None):
        super().__init__()
        self.button = QPushButton(btn_name)
        self.button.setCursor(
            QCursor(QtCore.Qt.CursorShape.PointingHandCursor))
        self.button.setStyleSheet(
            """*{border: 4px solid 'green';
            border-radius: 45px;
            font-size: 35px;
            color: 'white';
            padding: 25px 0;
            margin: 100px, 200px}
            *:hover{background: 'green'}
            *:pressed{background: 'lightgreen'}"""
        )

Then create the window class like this

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.window = QWidget()
        self.window.resize(500, 500)
        self.window.setWindowTitle("Pantuflis Software")
        self.window.setFixedWidth(1000)
        self.window.setStyleSheet("background: 'black';")
        self.grid = QGridLayout()
        self.window.setLayout(self.grid)
        self.button = Button("Play")
        self.grid.addWidget(self.button)
        self.window.show()

Finally add the rest

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec())

But the button doesn't show up, only the main window does. I also tried the same but without creataing the button from my own class and works. Must be something wrong in the button class but i can't see what is.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Pantuflis
  • 11
  • 1
  • 4

1 Answers1

1

If you are going to implement inherence then you have to apply the changes to the class. In your case it has a class that inherits from QPushButton but where you create the custom button which is not necessary, the same with the main window. My recommendation is that the OP should review his notes about inheritance.

import sys

from PyQt6.QtCore import Qt
from PyQt6.QtGui import QCursor
from PyQt6.QtWidgets import QApplication, QGridLayout, QPushButton, QWidget


class Button(QPushButton):
    def __init__(self, btn_name=""):
        super().__init__(btn_name)
        self.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
        self.setStyleSheet(
            """*{border: 4px solid 'green';
            border-radius: 45px;
            font-size: 35px;
            color: 'white';
            padding: 25px 0;
            margin: 100px, 200px}
            *:hover{background: 'green'}
            *:pressed{background: 'lightgreen'}"""
        )


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(500, 500)
        self.setWindowTitle("Pantuflis Software")
        self.setFixedWidth(1000)
        self.setStyleSheet("background: 'black';")
        self.grid = QGridLayout(self)
        self.button = Button("Play")
        self.grid.addWidget(self.button)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241