PyQt5生成子类小部件

2024-10-02 22:35:23 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我想知道我怎样才能生成一个widget的子类 例如,如果我想创建一个小部件,它继承了qtwidges.QPushButton,不过,我会在此基础上创建额外的方法和属性。在

class Coord(QtWidgets.QPushButton):
    def __init__(self):
        super(Coord, self).__init__()
        self.coordinates = []
        #basically adding attributes to the object "QPushButton"

    def set_text(self,text):
        self.setText(text)
        chrcount = 100 / len(text)
        self.setStyleSheet("font-size: {}".format(chrcount))
        #This will set the text of the button, yet will resize it appropriatly

这是一个例子。但是,它将“button”小部件创建为一个新窗口。我想知道如何让它像QPushButton那样工作,只需要添加一些额外的功能

编辑:固定- 替换了我的“超级”功能

^{pr2}$

def __init__(self,parent):
    super(Coord, self).__init__(parent)

不知道是怎么修好的,但是嘿嘿!在


Tags: thetextself功能init部件defbutton