Пример #1
0
    class HomeButton(QToolButton):
        def __init__(self, parent=None):
            QToolButton.__init__(self)
            self.setParent(parent)\

            # Set the layout of the button
            layout = QGridLayout()

            # Button formatting
            self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
            self.setAutoRaise(True)

            # Icon part of the button
            self.icon = QLabel()
            self.icon.setAlignment(Qt.AlignCenter)
            self.icon.setSizePolicy(QSizePolicy.Expanding,
                                    QSizePolicy.Expanding)
            self.iconPixmap = QPixmap()

            # Text part of the button
            # Initialise label
            self.text = QLabel()
            self.text.setAlignment(Qt.AlignBottom)
            # TODO: Change colour with universal theme
            self.text.setStyleSheet("color: white")
            # Add font styling
            font = QFont()
            font.setPixelSize(15)
            self.text.setFont(font)

            # Add components to the layer
            layout.addWidget(self.icon, 0, 0, 3, 3)
            layout.addWidget(self.text, 2, 0, 1, 3)
            self.setLayout(layout)

        def setIcon(self, path):
            # Set the icon part of the button
            # path: (str) path to the new icon to use
            self.icon.setPixmap(QPixmap(path))
            self.icon.repaint()

        def setText(self, text):
            # Set the text part of the button
            # text: (str) what the new text part should say
            self.text.setText(text)

        def setColour(self, r, g, b):
            # Set the colour to rgb
            # r: (int) red value
            # g: (int) green value
            # b: (int) blue value
            self.setStyleSheet(
                "QToolButton {{ background-color:  rgba({}, {}, {}, 0.8)}}\n\nQToolButton:pressed {{ background-color: rgba({}, {}, {}, 1)}}"
                .format(r, g, b, r, g, b))