Newbie - subclass QPushButton missing data
-
First post here. I am a newbie attempting to learn Python / PyQt5 and, if I understand the terminology correctly, subclass QPushButton. The code I have below creates a button, seems to assign the input data I pass in, and responds to a mouse click. However, the button shows on the main window without a label, tool tip, and sizing that I pass to "myCustomButton". Using PyCharm 2021.2 CE on a Mac.
Why does the button not have a label, tool tip, the wrong size, as seen in the main window and how do I fix this?
import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QMainWindow, QLabel from PyQt5.QtCore import pyqtSlot class myCustomButton(QPushButton): def __init__(self, text, tooltip, height=50, width=100, parent=None): QPushButton.__init__(self) self.button = QPushButton() self.button.setFixedSize(height, width) self.button.setText(text) self.button.setToolTip(tooltip) print("Button text: ", self.button.text()) print("Button tooltip: ", self.button.toolTip()) print("Button height: ", self.button.height()) print("Button width: ", self.button.width()) print("Button created.\n") class App(QMainWindow): def __init__(self): super().__init__() self.title = 'PyQt5 button test' self.left = 500 self.top = 300 self.width = 320 self.height = 200 self.initUI() def initUI(self): self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) layout = QVBoxLayout() widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) button = myCustomButton("My Custom Button", 'This is an custom button tool tip', 80, 40) button.clicked.connect(self.on_click) layout.addWidget(QLabel("Label above button.")) layout.addWidget(button) layout.addWidget(QLabel("Label below button.")) self.show() @pyqtSlot() def on_click(self): print('Custom button clicked.') if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_())
Thanks.
-
First post here. I am a newbie attempting to learn Python / PyQt5 and, if I understand the terminology correctly, subclass QPushButton. The code I have below creates a button, seems to assign the input data I pass in, and responds to a mouse click. However, the button shows on the main window without a label, tool tip, and sizing that I pass to "myCustomButton". Using PyCharm 2021.2 CE on a Mac.
Why does the button not have a label, tool tip, the wrong size, as seen in the main window and how do I fix this?
import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QMainWindow, QLabel from PyQt5.QtCore import pyqtSlot class myCustomButton(QPushButton): def __init__(self, text, tooltip, height=50, width=100, parent=None): QPushButton.__init__(self) self.button = QPushButton() self.button.setFixedSize(height, width) self.button.setText(text) self.button.setToolTip(tooltip) print("Button text: ", self.button.text()) print("Button tooltip: ", self.button.toolTip()) print("Button height: ", self.button.height()) print("Button width: ", self.button.width()) print("Button created.\n") class App(QMainWindow): def __init__(self): super().__init__() self.title = 'PyQt5 button test' self.left = 500 self.top = 300 self.width = 320 self.height = 200 self.initUI() def initUI(self): self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) layout = QVBoxLayout() widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) button = myCustomButton("My Custom Button", 'This is an custom button tool tip', 80, 40) button.clicked.connect(self.on_click) layout.addWidget(QLabel("Label above button.")) layout.addWidget(button) layout.addWidget(QLabel("Label below button.")) self.show() @pyqtSlot() def on_click(self): print('Custom button clicked.') if __name__ == '__main__': app = QApplication(sys.argv) ex = App() sys.exit(app.exec_())
Thanks.
@StarshipJohn You are creating a button within the custom button, that is not inheritance but composition (unnecessary in this case). Change to:
class myCustomButton(QPushButton): def __init__(self, text, tooltip, height=50, width=100, parent=None): QPushButton.__init__(self) self.setFixedSize(height, width) self.setText(text) self.setToolTip(tooltip) print("Button text: ", self.text()) print("Button tooltip: ", self.toolTip()) print("Button height: ", self.height()) print("Button width: ", self.width()) print("Button created.\n")
-
So close, so far away, so much to learn. Thank you.!