Why can't the button be set size and other styles?

Heads up! You've already completed this tutorial.

8942842346404 | 2021-06-08 02:08:48 UTC | #1

Why can't the button be set size and other styles? After using the design tool, the button control cannot be inherited because it cannot be sized.

python
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QSize

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(345, 225)
        self.pushButton = QtWidgets.QPushButton(self)
        self.pushButton.setGeometry(QtCore.QRect(20, 30, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "PushButt"))

class MyButton(QPushButton):
    def __init__(self):
        super().__init__()
        self.setStyleSheet("background:green")  #Writing this way will not produce practical results
        self.setFixedSize(QSize(10,10)) #Writing this way will not produce practical results

class Demo(Ui_Form,QWidget):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.btn = MyButton()
        self.btn.setFixedSize(QSize(100,20))  #Writing this way will not produce practical results

if __name__ == '__main__':
    app = QApplication([])
    w = Demo()
    w.show()
    sys.exit(app.exec_())

28|348x252


The complete guide to packaging Python GUI applications with PyInstaller.
[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Why can't the button be set size and other styles? was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.