1、效果展示
2、控件源码
import sys
from PyQt5.QtCore import Qt, QRect, QPoint, QVariantAnimation
from PyQt5.QtGui import QPainter, QColor
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout
class SwitchButton(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedSize(50, 30)
self.checked = False
self.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