Python PyQt5,如何使用QabStretchButton创建图像切换按钮

Python PyQt5,如何使用QabStretchButton创建图像切换按钮,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,我有一个基于QAbtractButton的PicButton类,它有normal、hover和pressed。但是,单击按钮时,按钮仅会变为短暂按下的pixmap_,然后切换回标准 如何使其行为类似于切换按钮,以使按下的pixmap在按下后保持不变 import numpy as np import time, sys from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * from P

我有一个基于QAbtractButton的PicButton类,它有normal、hover和pressed。但是,单击按钮时,按钮仅会变为短暂按下的pixmap_,然后切换回标准

如何使其行为类似于切换按钮,以使按下的pixmap在按下后保持不变

import numpy as np
import time, sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QMainWindow

class PicButton(QAbstractButton):
    def __init__(self, pixmap, pixmap_hover, pixmap_pressed, parent=None):
        super(PicButton, self).__init__(parent)
        self.pixmap = pixmap
        self.pixmap_hover = pixmap_hover
        self.pixmap_pressed = pixmap_pressed

        self.pressed.connect(self.update)
        # self.released.connect(self.update)

    def paintEvent(self, event):
        pix = self.pixmap_hover if self.underMouse() else self.pixmap
        if self.isDown():
            pix = self.pixmap_pressed
        painter = QPainter(self)
        painter.drawPixmap(event.rect(), pix)

    def enterEvent(self, event):
        self.update()

    def leaveEvent(self, event):
        self.update()

    def sizeHint(self):
        return self.pixmap.size()

class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.left = 0
        self.top = 0
        self.width = 800
        self.height = 800
        self.initUI()

    def initUI(self):
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.recBtn = PicButton(QPixmap('./img/playrecstop/rec_512.png'),QPixmap('./img/playrecstop/recHL_512.png'),\
            QPixmap('./img/playrecstop/recActive_512.png'))
        self.recBtn.setText("rec")
        self.recBtn.clicked.connect(self.controlButtons)

        self.stopBtn = PicButton(QPixmap('./img/playrecstop/stop_512.png'), QPixmap('./img/playrecstop/stopHL_512.png'),\
            QPixmap('./img/playrecstop/stopActive_512.png'))
        self.stopBtn.setText("stop")
        self.stopBtn.clicked.connect(self.controlButtons)

        self.leftLayout = QHBoxLayout()
        self.rightLayout = QHBoxLayout()

        self.rightLayout.addWidget(self.recBtn)
        self.rightLayout.addWidget(self.stopBtn)

        self.mainLayout = QHBoxLayout()
        self.mainLayout.addLayout(self.leftLayout)
        self.mainLayout.addLayout(self.rightLayout)

        self.setCentralWidget(QWidget(self))
        self.centralWidget().setLayout(self.mainLayout)
        self.show()


    def controlButtons(self):
        sender = self.sender()
        if (sender.text() == 'stop'):
            print ("Stop")
        elif (sender.text() == 'rec'):
            print ("REC...")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_()) 
谢谢

QAbstractButton有一个属性,允许您实现所需的逻辑:

class PicButton(QAbstractButton):
    def __init__(self, pixmap, pixmap_hover, pixmap_pressed, parent=None):
        super(PicButton, self).__init__(parent)
        self.pixmap = pixmap
        self.pixmap_hover = pixmap_hover
        self.pixmap_pressed = pixmap_pressed
        self.setCheckable(True)

    def paintEvent(self, event):
        pix = self.pixmap_hover if self.underMouse() else self.pixmap
        if self.isChecked():
            pix = self.pixmap_pressed
        painter = QPainter(self)
        painter.drawPixmap(event.rect(), pix)

    def enterEvent(self, event):
        self.update()

    def leaveEvent(self, event):
        self.update()

    def sizeHint(self):
        return self.pixmap.size()