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


1:1 Coaching & Tutoring for your Python GUIs project
Martin Fitzpatrick Python GUIs Coaching & Training
60 mins ($195) Book Now

1:1 Python GUIs Coaching & Training

Comprehensive code reviewBugfixes & improvements • Maintainability advice and architecture improvements • Design and usability assessment • Suggestions and tips to expand your knowledgePackaging and distribution help for Windows, Mac & Linux • Find out more.

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak
Martin Fitzpatrick

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. Martin founded PythonGUIs to provide easy to follow GUI programming tutorials to the Python community. He has written a number of popular Python books on the subject.