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(MyButton, self).__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(Demo,self).__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


PyQt/PySide 1:1 Coaching with Martin Fitzpatrick — Get one on one help with your Python GUI projects. Working together with you I'll identify issues and suggest fixes, from bugs and usability to architecture and maintainability.

More info 60 mins ($195)

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.