<filename>widgets/line_edit/three_state_button.py
# coding:utf-8
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import QToolButton


class ThreeStateButton(QToolButton):
    """ 三种状态对应三种图标的按钮 """

    def __init__(self, iconPath_dict: dict, parent=None):
        """
        Parameters
        ----------
        iconPath_dict: dict
            提供按钮 normal、hover、pressed 三种状态下的图标地址

        parent:
            父级窗口 """
        super().__init__(parent)
        # 引用图标地址字典
        self.iconPath_dict = iconPath_dict
        self.resize(QPixmap(self.iconPath_dict['normal']).size())
        # 初始化小部件
        self.initWidget()

    def initWidget(self):
        """ 初始化小部件 """
        self.setCursor(Qt.ArrowCursor)
        self.setIcon(QIcon(self.iconPath_dict['normal']))
        self.setIconSize(QSize(self.width(), self.height()))
        self.setStyleSheet('border: none; margin: 0px')

    def enterEvent(self, e):
        """ hover时更换图标 """
        self.setIcon(QIcon(self.iconPath_dict['hover']))

    def leaveEvent(self, e):
        """ leave时更换图标 """
        self.setIcon(QIcon(self.iconPath_dict['normal']))

    def mousePressEvent(self, e):
        """ 鼠标左键按下时更换图标 """
        if e.button() == Qt.RightButton:
            return
        self.setIcon(QIcon(self.iconPath_dict['pressed']))
        super().mousePressEvent(e)

    def mouseReleaseEvent(self, e):
        """ 鼠标左键松开时更换图标 """
        if e.button() == Qt.RightButton:
            return
        self.setIcon(QIcon(self.iconPath_dict['normal']))
        super().mouseReleaseEvent(e)

<filename>widgets/line_edit/three_state_button.py
# coding:utf-8
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import QToolButton


class ThreeStateButton(QToolButton):
    """ 三种状态对应三种图标的按钮 """

    def __init__(self, iconPath_dict: dict, parent=None):
        """
        Parameters
        ----------
        iconPath_dict: dict
            提供按钮 normal、hover、pressed 三种状态下的图标地址

        parent:
            父级窗口 """
        super().__init__(parent)
        # 引用图标地址字典
        self.iconPath_dict = iconPath_dict
        self.resize(QPixmap(self.iconPath_dict['normal']).size())
        # 初始化小部件
        self.initWidget()

    def initWidget(self):
        """ 初始化小部件 """
        self.setCursor(Qt.ArrowCursor)
        self.setIcon(QIcon(self.iconPath_dict['normal']))
        self.setIconSize(QSize(self.width(), self.height()))
        self.setStyleSheet('border: none; margin: 0px')

    def enterEvent(self, e):
        """ hover时更换图标 """
        self.setIcon(QIcon(self.iconPath_dict['hover']))

    def leaveEvent(self, e):
        """ leave时更换图标 """
        self.setIcon(QIcon(self.iconPath_dict['normal']))

    def mousePressEvent(self, e):
        """ 鼠标左键按下时更换图标 """
        if e.button() == Qt.RightButton:
            return
        self.setIcon(QIcon(self.iconPath_dict['pressed']))
        super().mousePressEvent(e)

    def mouseReleaseEvent(self, e):
        """ 鼠标左键松开时更换图标 """
        if e.button() == Qt.RightButton:
            return
        self.setIcon(QIcon(self.iconPath_dict['normal']))
        super().mouseReleaseEvent(e)

# coding:utf-8 三种状态对应三种图标的按钮 Parameters         ----------         iconPath_dict: dict             提供按钮 normal、hover、pressed 三种状态下的图标地址          parent:             父级窗口 # 引用图标地址字典 # 初始化小部件 初始化小部件 hover时更换图标 leave时更换图标 鼠标左键按下时更换图标 鼠标左键松开时更换图标