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.
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_())

PyQt/PySide 1:1 Coaching with Martin Fitzpatrick
Save yourself time and frustration. 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.