first commit
This commit is contained in:
73
toggleswitch.py
Normal file
73
toggleswitch.py
Normal file
@ -0,0 +1,73 @@
|
||||
from PySide6.QtWidgets import QApplication, QWidget, QLabel
|
||||
from PySide6.QtCore import QPropertyAnimation, QRect, QSize, Signal
|
||||
|
||||
|
||||
class ToggleSwitch(QWidget):
|
||||
clicked = Signal(int,bool)
|
||||
def __init__(self,parent=None):
|
||||
super().__init__(parent)
|
||||
self.setFixedSize(QSize(60, 30)) # 设置开关整体大小
|
||||
|
||||
# 背景标签(用于颜色变化)
|
||||
self.background = QLabel(self)
|
||||
self.background.setFixedSize(QSize(60, 30))
|
||||
self.background.setStyleSheet("background-color: #ccc; border-radius: 15px;")
|
||||
self.background.move(0, 0)
|
||||
|
||||
# 滑动圆圈
|
||||
self.circle = QLabel(self)
|
||||
self.circle.setFixedSize(26, 26)
|
||||
self.circle.setStyleSheet("background-color: white; border-radius: 13px;")
|
||||
self.circle.move(2, 2) # 初始位置
|
||||
|
||||
# 开关状态
|
||||
self.is_on = False
|
||||
|
||||
# 动画效果
|
||||
self.animation = QPropertyAnimation(self.circle, b"geometry")
|
||||
self.animation.setDuration(200)
|
||||
|
||||
# 点击事件
|
||||
self.mousePressEvent = self.toggle # 绑定点击事件
|
||||
|
||||
|
||||
def init(self,index):
|
||||
self.index = index
|
||||
|
||||
def toggle(self, event):
|
||||
# 切换开关状态
|
||||
|
||||
|
||||
# # if self.is_on:
|
||||
# # print("d")
|
||||
self.clicked.emit(self.index,not self.is_on)
|
||||
# # 更新背景颜色和动画位置
|
||||
# if self.is_on:
|
||||
# self.background.setStyleSheet("background-color: #4CAF50; border-radius: 15px;")
|
||||
# self.animation.setStartValue(QRect(2, 2, 26, 26))
|
||||
# self.animation.setEndValue(QRect(32, 2, 26, 26))
|
||||
# else:
|
||||
# self.background.setStyleSheet("background-color: #ccc; border-radius: 15px;")
|
||||
# self.animation.setStartValue(QRect(32, 2, 26, 26))
|
||||
# self.animation.setEndValue(QRect(2, 2, 26, 26))
|
||||
#
|
||||
# # 开始动画
|
||||
# self.animation.start()
|
||||
pass
|
||||
|
||||
def change_status(self):
|
||||
self.is_on = not self.is_on
|
||||
if self.is_on:
|
||||
self.background.setStyleSheet("background-color: #4CAF50; border-radius: 15px;")
|
||||
# self.animation.setStartValue(QRect(2, 2, 26, 26))
|
||||
# self.animation.setEndValue(QRect(32, 2, 26, 26))
|
||||
self.circle.move(32, 2) # 初始位置
|
||||
else:
|
||||
self.background.setStyleSheet("background-color: #ccc; border-radius: 15px;")
|
||||
# self.animation.setStartValue(QRect(32, 2, 26, 26))
|
||||
# self.animation.setEndValue(QRect(2, 2, 26, 26))
|
||||
self.circle.move(2,2)
|
||||
|
||||
# 开始动画
|
||||
#self.animation.start()
|
||||
|
||||
Reference in New Issue
Block a user