1、效果展示
2、控件源码
import sys
from PyQt5.QtCore import Qt, QRect, QPoint, QVariantAnimation
from PyQt5.QtGui import QPainter, QColor
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayoutclass SwitchButton(QWidget):def __init__(self, parent=None):super().__init__(parent)self.setFixedSize(50, 30)self.checked = Falseself.setCursor(Qt.PointingHandCursor)self.animation = QVariantAnimation()self.animation.setDuration(80) # 动画持续时间self.animation.setStartValue(0)self.animation.setEndValue(20)self.animation.valueChanged.connect(self.update)self.animation.finished.connect(self.onAnimationFinished)def setText(self, value):pass