如何在悬停时平滑更改背景颜色?

时间:2020-03-18 14:46:48

标签: python pyqt pyqt5

我的代码

import sys

from PyQt5.QtGui import QIcon, QPixmap
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
    QRect, QSize, QUrl, Qt)
from PyQt5.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
    QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
    QRadialGradient)
from PyQt5.QtWidgets import *

class RadioButton(QRadioButton): 
    def __init__(self, parent=None):
        super(RadioButton, self).__init__(parent)
        self.toggled.connect(self.change_text)
        self.setObjectName(u"rb0")
        self.setText('W')
        self.step = 0

    def change_text(self):
        if self.isChecked():                                   
            self.setText('C')
        else:
            self.setText('W')


    def enterEvent(self,event):
        while self.step < 255:
            self.step += 1
            print(self.step)
            self.setStyleSheet("QRadioButton{"+"background-color:rgb(255, {}, 0)".format(self.step)+";}QRadioButton::indicator {width:1 ;height:1; background:rgba(255, 255, 255, 0); }")

    def leaveEvent(self,event):
        while self.step > 0:
            self.step -= 1
            print(self.step)
            self.setStyleSheet("QRadioButton{"+"background-color:rgb(255, {}, 0)".format(self.step)+";}QRadioButton::indicator {width:1 ;height:1; background:rgba(255, 255, 255, 0); }")
    def sizeHint(self):
        return QtCore.QSize(30, 30)      


class Window(QWidget):
    def __init__(self):
        super().__init__()

        self._dictRB = {                                            
            'rb0': False,
            'rb1': False,
            'rb2': False,
            'rb3': False,
        }

        self.main_layout = QWidget(self)
        self.main_layout.setMinimumSize(QSize(300, 300))


        self.buttonGroup = QButtonGroup()
        self.attr_layout = QHBoxLayout(self.main_layout)


        self.rb0 = RadioButton()                             #QRadioButton() # 'rb0'
        self.rb0.setStyleSheet('QRadioButton{background: red} QRadioButton::indicator {width:1 ;height:1; background:rgba(255, 255, 255, 0); }')
        self.attr_layout.addWidget(self.rb0)
        self.buttonGroup.addButton(self.rb0)

        sizePolicy0 = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        sizePolicy0.setHorizontalStretch(0)
        sizePolicy0.setVerticalStretch(0)
        sizePolicy0.setHeightForWidth(self.rb0.sizePolicy().hasHeightForWidth())
        self.rb0.setSizePolicy(sizePolicy0)
        self.rb1 = QRadioButton('rb1')
        self.rb1.setObjectName(u"rb1")

        sizePolicy1 = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        sizePolicy1.setHorizontalStretch(0)
        sizePolicy1.setVerticalStretch(0)
        sizePolicy1.setHeightForWidth(self.rb1.sizePolicy().hasHeightForWidth())
        self.rb1.setSizePolicy(sizePolicy1)

        self.attr_layout.addWidget(self.rb1)
        self.buttonGroup.addButton(self.rb1)               

        self.rb2 = QRadioButton('rb2')
        self.rb2.setObjectName(u"rb2")
        self.attr_layout.addWidget(self.rb2)
        self.buttonGroup.addButton(self.rb2) 


        sizePolicy2 = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        sizePolicy2.setHorizontalStretch(0)
        sizePolicy2.setVerticalStretch(0)
        sizePolicy2.setHeightForWidth(self.rb2.sizePolicy().hasHeightForWidth())
        self.rb2.setSizePolicy(sizePolicy2)


        self.rb3 = QRadioButton('rb3')
        self.rb3.setObjectName(u"rb3")
        self.buttonGroup.addButton(self.rb3)                         


        sizePolicy3 = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        sizePolicy3.setHorizontalStretch(0)
        sizePolicy3.setVerticalStretch(0)
        sizePolicy3.setHeightForWidth(self.rb3.sizePolicy().hasHeightForWidth())
        self.rb3.setSizePolicy(sizePolicy3)

        self.buttonGroup.buttonClicked.connect(self.check_button)

    def check_button(self, radioButton):
        if self._dictRB[radioButton.objectName()]:
            self._dictRB[radioButton.objectName()] = False
            self._dictRB['rb3'] = True
            self.rb3.setChecked(True)              
        else:
            for b in self._dictRB:
                self._dictRB[b] = False
            self._dictRB[radioButton.objectName()] = True

        print("Нажата кнопка -> `{} - {}`".format(radioButton.objectName(), radioButton.isChecked()))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())

我需要在悬停时平滑地更改颜色

image

我尝试使用enterEvent和离开事件来执行此操作 但是它们仅在鼠标位于并离开该区域时才注册一个动作。

我尝试过使用一段时间,但是颜色平滑变化不想要

qt中是否有任何工具可让您在悬停时平滑地更改颜色 并且不要不断重写setStyleSheet吗?

0 个答案:

没有答案
相关问题