效果展示
代码展示
from PyQt5.QtCore import Qt, pyqtSignal, QTimer, QRectF, QRect
from PyQt5.QtGui import QFont, QColor, QPainter, QPainterPath
from PyQt5.QtWidgets import QWidget
class SwitchBtn(QWidget):
clicked = pyqtSignal(bool)
BORDER_WIDTH = 4
COLOR_BORDER = (209, 207, 198)
COLOR_SLIDER = (255, 255, 255)
COLOR_TEXT = (255, 255, 255)
COLOR_CLOSE = (112, 112, 112)
COLOR_OPEN = (0, 77, 191)
COLOR_ALPHA = 150
SPEED = 0.008
TEXT_ON = 'ON'
TEXT_OFF = 'OFF'
FONT_FAMILY = '微软雅黑'
FONT_SIZE = 9
def __init__(self, parent=None):
super().__init__(parent)
self.__checked = False
self.__enabled = True
self.__step = self.width() * self.SPEED
self.__start = 0
self.__end = 0
self.__color_border = QColor(*self.COLOR_BORDER)
self.__color_slider = QColor(*self.COLOR_SLIDER)
self.__color_back = QColor(*self.COLOR_CLOSE)
self.__color_text = QColor(*self.COLOR_TEXT)
self.__timer_slider = QTimer()
self.__timer_slider.setInterval(1)
self.__timer_slider.timeout.connect(self.__to_slider)
self.__font = QFont()
self.__font.setFamily(self