class FileManager(QDialog): def __init__(self): super().__init__() self.ui = Ui_AI() self.ui.setupUi(self) self.show() self.Loop() def Loop(self): self.light = QPropertyAnimation(self.ui.HologramLight, b"geometry") self.light.setDirection(1) self.light.setLoopCount(2) self.light.setStartValue(QRect(0, 781, 481, 20)) self.light.setEndValue(QRect(0, 340, 481, 461)) self.light.start() self.light.stop() self.anim = QPropertyAnimation(self.ui.Blur2, b"geometry") self.anim.setDirection(1) self.anim.setLoopCount(100) self.anim.setStartValue(QRect(110, 240, 231, 231)) self.anim.setEndValue(QRect(130, 240, 231, 231)) self.anim.start() mixer.init() mixer.music.load('/root/PycharmProjects/AI/good.mp3') mixer.music.play()
def fanAnimation(self): anim = QPropertyAnimation(self, b'rotation') anim.setDuration(1000) anim.setStartValue(0) anim.setEndValue(360) anim.setLoopCount(-1) return anim
class UQline(UQtxt): def __init__(self,*args,**kwargs): super().__init__(*args,**kwargs) kwargs = self._args(*args,**kwargs) redim = kwargs.get("redim",None) posi = kwargs.get("posi",None) size = kwargs.get("size",None) anim = kwargs.get("anim",True) w = 5 if redim == "w": self.resize(size,self.height()) if redim == "h": self.resize(self.width(),size) self.move(posi[0],posi[1]) ef = QGraphicsColorizeEffect(self) self.setGraphicsEffect(ef) self.__animation = QPropertyAnimation(ef,b"color") self.__animation.setDuration(2000) self.__animation.setKeyValueAt(0,QColor(0, 255, 0)) self.__animation.setKeyValueAt(0.5,QColor(255, 0, 0)) self.__animation.setKeyValueAt(1,QColor(0, 255, 0)) self.__animation.setLoopCount(-1) if anim: self.__animation.start() self.show() def load_anim(self): self.__animation.start() def stop_anim(self): self.__animation.stop()
def doShakeWindow(self, target): """窗口抖动动画 :param target: 目标控件 """ if hasattr(target, '_shake_animation'): # 如果已经有该对象则跳过 return animation = QPropertyAnimation(target, b'pos', target) target._shake_animation = animation animation.finished.connect(lambda: delattr(target, '_shake_animation')) pos = target.pos() x, y = pos.x(), pos.y() animation.setDuration(200) animation.setLoopCount(2) animation.setKeyValueAt(0, QPoint(x, y)) animation.setKeyValueAt(0.09, QPoint(x + 2, y - 2)) animation.setKeyValueAt(0.18, QPoint(x + 4, y - 4)) animation.setKeyValueAt(0.27, QPoint(x + 2, y - 6)) animation.setKeyValueAt(0.36, QPoint(x + 0, y - 8)) animation.setKeyValueAt(0.45, QPoint(x - 2, y - 10)) animation.setKeyValueAt(0.54, QPoint(x - 4, y - 8)) animation.setKeyValueAt(0.63, QPoint(x - 6, y - 6)) animation.setKeyValueAt(0.72, QPoint(x - 8, y - 4)) animation.setKeyValueAt(0.81, QPoint(x - 6, y - 2)) animation.setKeyValueAt(0.90, QPoint(x - 4, y - 0)) animation.setKeyValueAt(0.99, QPoint(x - 2, y + 2)) animation.setEndValue(QPoint(x, y)) animation.start(animation.DeleteWhenStopped)
class AnimationShadowEffect(QGraphicsDropShadowEffect): def __init__(self, color, *args, **kwargs): super(AnimationShadowEffect, self).__init__(*args, **kwargs) self.setColor(color) self.setOffset(0, 0) self.setBlurRadius(0) self._radius = 0 self.animation = QPropertyAnimation(self) self.animation.setTargetObject(self) self.animation.setDuration(2000) # 一次循环时间 self.animation.setLoopCount(-1) # 永久循环 self.animation.setPropertyName(b'radius') # 插入值 self.animation.setKeyValueAt(0, 1) self.animation.setKeyValueAt(0.5, 30) self.animation.setKeyValueAt(1, 1) def start(self): self.animation.start() def stop(self, r=0): # 停止动画并修改半径值 self.animation.stop() self.radius = r @pyqtProperty(int) def radius(self): return self._radius @radius.setter def radius(self, r): self._radius = r self.setBlurRadius(r)
def bobble(pix): node = Node(pix) pos = node.pix.pos() node.pix.setOriginPt() sync = random.randint(4, 9) * 400 up = random.randint(5, 12) left = random.randint(5, 9) if random.randint(0, 1): up = up * -1 if random.randint(0, 1): left = left * -1 bobble = QPropertyAnimation(node, b'pos') bobble.setDuration(sync) bobble.setStartValue(pos) bobble.setKeyValueAt(0.25, pos + QPointF(-left, -up * 1.75)) bobble.setKeyValueAt(0.35, pos + QPointF(-left * 1.35, 0)) bobble.setKeyValueAt(0.45, pos + QPointF(0, up * 1.65)) bobble.setKeyValueAt(0.55, pos + QPointF(left * 1.35, 0)) bobble.setKeyValueAt(0.65, pos + QPointF(left, -up * 1.75)) bobble.setEndValue(pos) bobble.setLoopCount(-1) bobble.setEasingCurve(QEasingCurve.InOutCubic) return bobble
class Demo(QWidget): def __init__(self): super(Demo, self).__init__() self.resize(600, 600) self.btn = QPushButton('Bigger', self) self.btn.setObjectName('btn') self.btn.resize(100, 100) self.animation = QPropertyAnimation(self) # 指定动画作用的对象 self.animation.setTargetObject(self.btn) # b'size' 改变大小 b'pos' 改变位置 self.animation.setPropertyName(b'pos') # 设置动画持续时长 self.animation.setDuration(1000) # 设置位置关键帧 self.animation.setKeyValueAt(0, self.btn.pos() + QPoint(0, 0)) self.animation.setKeyValueAt(0.2, self.btn.pos() + QPoint(0, 15)) self.animation.setKeyValueAt(0.4, self.btn.pos() + QPoint(0, 0)) self.animation.setKeyValueAt(0.6, self.btn.pos() + QPoint(0, -15)) self.animation.setKeyValueAt(0.8, self.btn.pos() + QPoint(0, 0)) # self.animation.setKeyValueAt(1, self.btn.pos() + QPoint(0, 0)) # 设置动画循环次数 self.animation.setLoopCount(3) QMetaObject.connectSlotsByName(self) @pyqtSlot() def on_btn_clicked(self): # 启动动画 self.animation.start()
class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): hbox = QHBoxLayout(self) self.button = QPushButton("Start", self) self.button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) hbox.addWidget(self.button) hbox.addSpacing(40) self.label = MyLabel("Summer") font = self.label.font() font.setPointSize(35) self.label.setFont(font) hbox.addWidget(self.label) self.anim = QPropertyAnimation(self.label, b"color") self.anim.setDuration(2500) self.anim.setLoopCount(2) self.anim.setStartValue(QColor(0, 0, 0)) self.anim.setEndValue(QColor(255, 255, 255)) self.button.clicked.connect(self.anim.start) self.setGeometry(300, 300, 380, 250) self.setWindowTitle('Color anim') self.show()
def move(self, r1: Rectangle, r2: Rectangle): r1.in_animation = True animation = QPropertyAnimation(r1, b"pos") animation.setDuration(self.duration) animation.setStartValue(QPointF(r1.x(), r1.y())) animation.setEndValue(QPointF(r2.x(), r2.y())) animation.setLoopCount(1) self.animations.append(animation) animation.start()
def move(self, car, px, py): animation = QPropertyAnimation(self, b'pos') animation.setParent(self) animation.setDuration(2000) animation.setTargetObject(car) animation.setStartValue(QPoint(100, 100)) animation.setEndValue(QPoint(100, 400)) animation.setLoopCount(1) # self.animation.finished.connect(self.animationFinished) animation.start()
class PushButtonFont(QPushButton): LoadingText = "\uf110" def __init__(self, *args, **kwargs): super(PushButtonFont, self).__init__(*args, **kwargs) self.fontSize = self.font().pointSize() * 2 self._rotateAnimationStarted = False self._rotateAnimation = QPropertyAnimation(self) self._rotateAnimation.setTargetObject(self) self._rotateAnimation.setStartValue(1) self._rotateAnimation.setEndValue(12) self._rotateAnimation.setDuration(1000) self._rotateAnimation.setLoopCount(-1) # 无限循环 self._rotateAnimation.valueChanged.connect(self.update) self.clicked.connect(self._onClick) def paintEvent(self, _): option = QStyleOptionButton() self.initStyleOption(option) painter = QStylePainter(self) if self._rotateAnimationStarted: option.text = "" painter.drawControl(QStyle.CE_PushButton, option) if not self._rotateAnimationStarted: return painter.save() font = self.font() font.setPointSize(self.fontSize) font.setFamily("FontAwesome") painter.setFont(font) # 变换坐标为正中间 painter.translate(self.rect().center()) # 旋转90度 painter.rotate(self._rotateAnimation.currentValue() * 30) fm = self.fontMetrics() # 在变换坐标后的正中间画文字 w = fm.width(self.LoadingText) h = fm.height() painter.drawText( QRectF(0 - w * 2, 0 - h, w * 2 * 2, h * 2), Qt.AlignCenter, self.LoadingText) painter.restore() def _onClick(self): if self._rotateAnimationStarted: self._rotateAnimationStarted = False self._rotateAnimation.stop() return self._rotateAnimationStarted = True self._rotateAnimation.start() def update(self, _=None): super(PushButtonFont, self).update()
def show_error_animation(self): animation = QPropertyAnimation(self) animation.setTargetObject(self.login_widget) animation.setPropertyName(b"pos") animation.setKeyValueAt(0, self.login_widget.pos()) animation.setKeyValueAt(0.2, self.login_widget.pos() - QPoint(15, 0)) animation.setKeyValueAt(0.5, self.login_widget.pos()) animation.setKeyValueAt(0.7, self.login_widget.pos() - QPoint(-15, 0)) animation.setKeyValueAt(1, self.login_widget.pos()) animation.setDuration(200) animation.setLoopCount(5) animation.start(QAbstractAnimation.DeleteWhenStopped)
def createScene(): # Root entity rootEntity = QEntity() # Material material = QPhongMaterial(rootEntity) # Torus torusEntity = QEntity(rootEntity) # Qt3DExtras.QTorusMesh * torusMesh = QTorusMesh() torusMesh.setRadius(5) torusMesh.setMinorRadius(1) torusMesh.setRings(100) torusMesh.setSlices(20) # Qt3DCore.QTransform * torusTransform = QTransform() torusTransform.setScale3D(QVector3D(1.5, 1, 0.5)) torusTransform.setRotation( QQuaternion.fromAxisAndAngle(QVector3D(1, 0, 0), 45.0)) torusEntity.addComponent(torusMesh) torusEntity.addComponent(torusTransform) torusEntity.addComponent(material) # Sphere sphereEntity = QEntity(rootEntity) sphereMesh = QSphereMesh() sphereMesh.setRadius(3) # Qt3DCore.QTransform * sphereTransform = QTransform() # OrbitTransformController * controller = OrbitTransformController(sphereTransform) controller.setTarget(sphereTransform) controller.setRadius(20.0) # QPropertyAnimation * sphereRotateTransformAnimation = QPropertyAnimation(sphereTransform) sphereRotateTransformAnimation.setTargetObject(controller) sphereRotateTransformAnimation.setPropertyName(b"angle") sphereRotateTransformAnimation.setStartValue(0) sphereRotateTransformAnimation.setEndValue(360) sphereRotateTransformAnimation.setDuration(10000) sphereRotateTransformAnimation.setLoopCount(-1) sphereRotateTransformAnimation.start() sphereEntity.addComponent(sphereMesh) sphereEntity.addComponent(sphereTransform) sphereEntity.addComponent(material) return rootEntity
def idle(pix): node = Node(pix) pos = node.pix.pos() sync = 1000 idle = QPropertyAnimation(node, b'pos') idle.setDuration(sync) idle.setStartValue(pos) idle.setEndValue(pos) idle.setLoopCount(-1) return idle
class Demo(QWidget): def __init__(self): super(Demo,self).__init__() self.resize(600,600) self.btn = MyButton('Color',self) self.btn.setGeometry(0,0,100,100) self.animation = QPropertyAnimation(self.btn, b'color') self.animation.setDuration(5000) self.animation.setStartValue(QColor(0, 0, 0)) self.animation.setEndValue(QColor(255, 255, 255)) self.animation.setLoopCount(-1) self.animation.start()
def spin(pix, anime, node): node.pix.setOriginPt() sync = random.randint(14, 23) * 50 rot = 360 if anime.endswith('Left'): rot = -360 spin = QPropertyAnimation(node, b'rotate') spin.setDuration(sync) spin.setStartValue(node.pix.rotation) spin.setEndValue(node.pix.rotation + rot) spin.setLoopCount(-1) return spin
class Demo(QWidget): def __init__(self): super(Demo, self).__init__() self.resize(600, 600) self.btn = QPushButton('Move', self) self.btn.setGeometry(0, 0, 100, 100) self.animation = QPropertyAnimation(self.btn, b'geometry') self.animation.setDuration(5000) self.animation.setStartValue(QRect(0, 0, 100, 100)) self.animation.setEndValue(QRect(300, 300, 300, 300)) # setEasingCurve()设置缓和曲线(即动画变化形式) self.animation.setEasingCurve(QEasingCurve.InBounce) # 调用setLoopCount()并传入-1来无限循环动画(传入正整数代表运行相应的次数,传入0不运行) self.animation.setLoopCount(-1) self.animation.start()
def pulse(pix): node = Node(pix) node.pix.setOriginPt() random.seed() sync = random.gauss(450, 50) pulse = QPropertyAnimation(node, b'scale') pulse.setDuration(sync) pulse.setStartValue(node.pix.scale * random.gauss(1.25, .25)) pulse.setEndValue(node.pix.scale * random.gauss(1.25, .25)) pulse.setLoopCount(-1) seq = QSequentialAnimationGroup() seq.addAnimation(pulse) seq.addPause(random.randint(10, 30) * 30) return seq
class Window(QMainWindow): def __init__(self): super().__init__() self.setStyleSheet("background: #fff;\n") self.title = "PyQt5 Window" self.top = 100 self.left = 100 self.width = 680 self.height = 500 self.InitWindow() def InitWindow(self): self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.top, self.left, self.width, self.height) self.button = QPushButton("Start", self) self.button.move(30, 30) self.button.clicked.connect(self.doAnimation) #self.frame = QFrame(self)setStyleSheet("background-image: url(/home/authoraytee/Documents/PyQT/anime/jojo_1);") #self.email_form.setStyleSheet("border-bottom: 2px solid #0072de;\n") self.frame1 = QFrame(self) self.frame1.setStyleSheet( "background-image: url(/home/authoraytee/Documents/PyQT/anime/jojo_6);\n" "border: none;\n") self.frame1.setFrameStyle(QFrame.Panel | QFrame.Raised) self.frame1.setGeometry(size, size, -50, -50) self.show() def doAnimation(self): self.anim = QPropertyAnimation(self.frame1, b"geometry") self.anim.setDuration(100) self.anim.setLoopCount(10) self.anim.setStartValue(QRect(-100, -100, size, size)) self.anim.setEndValue(QRect(0, 0, size, size)) self.anim.start()
def setPaths(pix, anime, node): pos = node.pix.pos() sync = random.randint(73,173) * 100 ## very arbitrary path = QPropertyAnimation(node, b'pos') path.setDuration(sync) waypts = pathLoader(anime) if not waypts: return ## offset for origin pt - setOrigin wasn't working pt = getOffset(node.pix) path.setStartValue(waypts.pointAtPercent(0.0)-pt) for i in range(1, 99): path.setKeyValueAt(i/100.0, waypts.pointAtPercent(i/100.0)-pt) path.setEndValue(waypts.pointAtPercent(1.0)-pt) path.setLoopCount(-1) return path
class RotatingTrefoilKnot(TrefoilKnot): def __init__(self, parent=None): super(RotatingTrefoilKnot, self).__init__(parent) self.m_thetaAnimation = QPropertyAnimation() self.m_thetaAnimation.setDuration(2000) self.m_thetaAnimation.setStartValue(0.0) self.m_thetaAnimation.setEndValue(360.0) self.m_thetaAnimation.setLoopCount(-1) self.m_thetaAnimation.setTargetObject(self) self.m_thetaAnimation.setPropertyName(b'phi') self.m_thetaAnimation.start() self.m_phiAnimation = QPropertyAnimation() self.m_phiAnimation.setDuration(2000) self.m_phiAnimation.setStartValue(0.0) self.m_phiAnimation.setEndValue(360.0) self.m_phiAnimation.setLoopCount(-1) self.m_phiAnimation.setTargetObject(self) self.m_phiAnimation.setPropertyName(b'theta') self.m_phiAnimation.start()
class Demo(QWidget): def __init__(self): super(Demo, self).__init__() self.resize(600, 600) self.plane = QLabel(self) self.plane.resize(50, 50) self.plane.setPixmap(QPixmap('plane.gif').scaled( self.plane.size())) # 1 self.animation1 = QPropertyAnimation(self.plane, b'geometry') self.animation1.setDuration(2000) self.animation1.setStartValue(QRect(300, 500, 50, 50)) self.animation1.setEndValue(QRect(200, 400, 50, 50)) self.animation1.setLoopCount(1) self.animation2 = QPropertyAnimation(self.plane, b'geometry') self.animation2.setDuration(2000) self.animation2.setStartValue(QRect(200, 400, 50, 50)) self.animation2.setEndValue(QRect(400, 300, 50, 50)) self.animation2.setLoopCount(1) self.animation3 = QPropertyAnimation(self.plane, b'geometry') self.animation3.setDuration(2000) self.animation3.setStartValue(QRect(400, 300, 50, 50)) self.animation3.setEndValue(QRect(200, 200, 50, 50)) self.animation3.setLoopCount(1) self.animation_group = QSequentialAnimationGroup(self) # 2 self.animation_group.addAnimation(self.animation1) self.animation_group.addPause(1000) self.animation_group.addAnimation(self.animation2) self.animation_group.addAnimation(self.animation3) self.animation_group.start()
class ApplWindow(QDialog): def __init__(self): super(ApplWindow, self).__init__() self.ui = uic.loadUi('testmoveLabel.ui', self) self.ui.pushButton.clicked.connect(self.runne) self.ui.pushButton_2.clicked.connect(self.checkne) self.show() def runne(self,x): self.CN = QPropertyAnimation(self.ui.label, b"geometry") self.CN.setDuration(4000) self.CN.setLoopCount(1) print('a') self.CN.setKeyValueAt(0,QRect(600, 0, 80, 120)) self.CN.setKeyValueAt(0.5,QRect(0, 0, 80, 120)) self.CN.setKeyValueAt(1,QRect(70, 200, 80, 120)) self.CN.start() def checkne(self): x=self.ui.label.x() x2=self.ui.label_2.x() print(x) print(x2) def stopx(self): self.CN.stop()
class Spinner(QLabel): ICON = None SIZE = 75 HALF_SIZE = SIZE / 2.0 def __init__(self, *args, **kwargs): super().__init__() if not Spinner.ICON: Spinner.ICON = QPixmap(":/images/spinner_two.png") self.setAlignment(Qt.AlignCenter) self.setFixedSize(Spinner.SIZE, Spinner.SIZE) self.setPixmap( Spinner.ICON.scaledToWidth(Spinner.SIZE, Qt.SmoothTransformation)) self.angle = 0 self.animation = QPropertyAnimation(self, b"rotation", self) self.animation.setStartValue(0) self.animation.setEndValue(360) self.animation.setLoopCount(-1) self.animation.setDuration(1250) self.animation.start() @pyqtProperty(float) def rotation(self): return self.angle @rotation.setter def rotation(self, value): self.angle = value self.update() def paintEvent(self, event=None): painter = QPainter(self) painter.translate(Spinner.HALF_SIZE, Spinner.HALF_SIZE) painter.rotate(self.angle) painter.translate(-Spinner.HALF_SIZE, -Spinner.HALF_SIZE) painter.drawPixmap(0, 0, self.pixmap())
def initUi(self): self.getSystemSettings() self.setWindowTitle('我的第一个Qt程序') self.resize(500, 300) # 把label放到父类里 self就是父类 即主窗口 LoginWindow # label = QLabel() # label.setParent(self) label = QLabel(self) label.setText("社会我杰哥,人狠话不多") label.move(100, 100) self.le = QLineEdit(self) self.le.setText(self.settings.value("username")) # 透明效果 animation = QPropertyAnimation(self) animation.setTargetObject(self) animation.setPropertyName(b"windowOpacity") animation.setStartValue(1) animation.setKeyValueAt(0.5, 0) animation.setEndValue(1) animation.setLoopCount(2) animation.setDuration(2000) animation.start(QAbstractAnimation.DeleteWhenStopped)
class ApplWindow(QDialog): def __init__(self): super(ApplWindow, self).__init__() self.ui = uic.loadUi('testmoveLabel.ui', self) self.ui.pushButton.clicked.connect(self.runne) self.ui.pushButton_2.clicked.connect(self.runne) self.count = True # self.left = [300,350,400,450] # self.top = 0 # self.width = 20 # self.height = [170,160,180,190] # for i in range(-1,3): # label_8 = QLabel(str("label_"+str(i+1)), self) # label_8.setStyleSheet("background:red") # label_8.setGeometry(self.left[i+1], self.top, self.width, self.height[i+1]) self.show() def runne(self, x): # self.lstR = [30, 190, 360] # a = random.choice(self.lstR) # self.CN = QPropertyAnimation(self.ui.label, b"geometry") # self.CN.setDuration(2000) # self.CN.setLoopCount(-1) # self.CN.setStartValue(QRect(0, 100, 130, 170)) # self.CN.setEndValue(QRect(400, 100, 130, 170)) # self.CN.start() # print(self.count) self.CN = QPropertyAnimation(self.ui.label, b"geometry") self.lstR = [-100] self.CN.setDuration(4000) self.CN.setLoopCount(-1) print('a') self.CN.setStartValue(QRect(600, 0, 80, 120)) self.CN.setEndValue(QRect(-100, 0, 80, 120)) self.CN.start() self.CN1 = QPropertyAnimation(self.ui.label_2, b"geometry") self.CN1.setDuration(4300) self.CN1.setLoopCount(-1) self.CN1.setStartValue(QRect(800, 100, 80, 140)) self.CN1.setEndValue(QRect(-50, 0, 80, 140)) self.CN1.start() print('b') self.CN2 = QPropertyAnimation(self.ui.label_3, b"geometry") self.CN2.setDuration(4600) self.CN2.setLoopCount(-1) self.CN2.setStartValue(QRect(1000, 0, 80, 135)) self.CN2.setEndValue(QRect(0, 0, 80, 135)) self.CN2.start() print('c') def stopx(self): self.CN.stop() self.CN1.stop() self.CN2.stop()
class VCBlinkText(QWidget): def __init__(self, text: str, parent=None): super(VCBlinkText, self).__init__(parent) self.label = QLabel(text) self.label.setMinimumHeight(self.label.sizeHint().height() + 20) layout = QHBoxLayout(self) layout.addWidget(self.label) self.effect = QGraphicsOpacityEffect(self.label) self.label.setGraphicsEffect(self.effect) self.anim = QPropertyAnimation(self.effect, b'opacity') self.anim.setDuration(3500) self.anim.setLoopCount(-1) self.anim.setStartValue(1.0) self.anim.setKeyValueAt(0.5, 0.0) self.anim.setEndValue(1.0) self.anim.setEasingCurve(QEasingCurve.OutQuad) self.anim.start(QPropertyAnimation.DeleteWhenStopped) def setAlignment(self, alignment: Qt.AlignmentFlag) -> None: self.label.setAlignment(alignment) def stop(self) -> None: self.anim.stop()
def vibrate(pix): node = Node(pix) pos = node.pix.pos() node.pix.setOriginPt() random.seed() sync = random.randint(130, 205) ran = random.randint(3, 7) * 2 vibrate = QPropertyAnimation(node, b'pos') vibrate.setDuration(sync) vibrate.setStartValue(pos) vibrate.setKeyValueAt(0.25, pos + QPointF(-ran, ran)) vibrate.setKeyValueAt(0.25, pos + QPointF(ran * 1.75, ran * 1.25)) vibrate.setKeyValueAt(0.25, pos + QPointF(-ran * 1.75, -ran * 1.25)) vibrate.setKeyValueAt(0.25, pos + QPointF(-ran, ran)) vibrate.setEndValue(pos) vibrate.setLoopCount(-1) vibrate.setEasingCurve(QEasingCurve.InOutBack) return vibrate
class Window(QWidget): def __init__(self, *args, **kwargs): super(Window, self).__init__(*args, **kwargs) layout = QVBoxLayout(self) layout.addWidget(QPushButton('start', self, clicked=self.onStart)) layout.addWidget(QPushButton('pause', self, clicked=self.onPause)) layout.addWidget(QPushButton('resume', self, clicked=self.onResume)) self._offset = 0 self.pani = QPropertyAnimation(self, b'offset', self) self.pani.setDuration(540) self.pani.setLoopCount(1) self.pani.setStartValue(0) self.pani.setEndValue(360) def onPause(self): self.pani.stop() v = self.pani.currentValue() print('current value:', v, 'duration:', int(v / 360 * 540)) self.pani.setDuration(int(v / 360 * 540)) self.pani.setStartValue(v) self.pani.setEndValue(0) def onResume(self): self.pani.start() def onStart(self): self.pani.start() @pyqtProperty(int) def offset(self): return self._offset @offset.setter def offset(self, o): print(o) self._offset = o
class RotateButton(QPushButton): STARTVALUE = 0 # 起始旋转角度 ENDVALUE = 360 # 结束旋转角度 DURATION = 540 # 动画完成总时间 def __init__(self, *args, image='', **kwargs): super(RotateButton, self).__init__(*args, **kwargs) self.setCursor(Qt.PointingHandCursor) self._angle = 0 # 角度 self._padding = 10 # 阴影边距 self._image = '' # 图片路径 self._shadowColor = QColor(33, 33, 33) # 阴影颜色 self._pixmap = None # 图片对象 # 属性动画 self._animation = QPropertyAnimation(self, b'angle', self) self._animation.setLoopCount(1) # 只循环一次 self.setPixmap(image) # 绑定提示框 #ToolTip.bind(self) def paintEvent(self, event): """绘制事件""" text = self.text() option = QStyleOptionButton() self.initStyleOption(option) option.text = '' # 不绘制文字 painter = QStylePainter(self) painter.setRenderHint(QStylePainter.Antialiasing) painter.setRenderHint(QStylePainter.HighQualityAntialiasing) painter.setRenderHint(QStylePainter.SmoothPixmapTransform) painter.drawControl(QStyle.CE_PushButton, option) # 变换坐标为正中间 painter.translate(self.rect().center()) painter.rotate(self._angle) # 旋转 # 绘制图片 if self._pixmap and not self._pixmap.isNull(): w = self.width() h = self.height() pos = QPointF(-self._pixmap.width() / 2, -self._pixmap.height() / 2) painter.drawPixmap(pos, self._pixmap) elif text: # 在变换坐标后的正中间画文字 fm = self.fontMetrics() w = fm.width(text) h = fm.height() rect = QRectF(0 - w * 2, 0 - h, w * 2 * 2, h * 2) painter.drawText(rect, Qt.AlignCenter, text) else: super(RotateButton, self).paintEvent(event) def enterEvent(self, _): """鼠标进入事件""" # 设置阴影 # 边框阴影效果 ''' effect = QGraphicsDropShadowEffect(self) effect.setBlurRadius(self._padding * 2) effect.setOffset(0, 0) effect.setColor(self._shadowColor) self.setGraphicsEffect(effect) ''' # 开启旋转动画 self._animation.stop() cv = self._animation.currentValue() or self.STARTVALUE self._animation.setDuration(self.DURATION if cv == 0 else int(cv / self.ENDVALUE * self.DURATION)) self._animation.setStartValue(cv) self._animation.setEndValue(self.ENDVALUE) self._animation.start() def leaveEvent(self, _): """鼠标离开事件""" # 取消阴影 self.setGraphicsEffect(None) # 旋转动画 self._animation.stop() cv = self._animation.currentValue() or self.ENDVALUE self._animation.setDuration(int(cv / self.ENDVALUE * self.DURATION)) self._animation.setStartValue(cv) self._animation.setEndValue(self.STARTVALUE) self._animation.start() def setPixmap(self, path): if not os.path.exists(path): self._image = '' self._pixmap = None return self._image = path size = min(self.width(), self.height()) - self.padding # 需要边距的边框 radius = int(size / 2) image = QImage(size, size, QImage.Format_ARGB32_Premultiplied) image.fill(Qt.transparent) # 填充背景为透明 pixmap = QPixmap(path).scaled(size, size, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation) # QPainter painter = QPainter() painter.begin(image) painter.setRenderHint(QPainter.Antialiasing, True) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) # QPainterPath path = QPainterPath() path.addRoundedRect(0, 0, size, size, radius, radius) # 切割圆 painter.setClipPath(path) painter.drawPixmap(0, 0, pixmap) painter.end() self._pixmap = QPixmap.fromImage(image) self.update() def pixmap(self): return self._pixmap @pyqtProperty(str) def image(self): return self._image @image.setter def image(self, path): self.setPixmap(path) @pyqtProperty(int) def angle(self): return self._angle @angle.setter def angle(self, value): self._angle = value self.update() @pyqtProperty(int) def padding(self): return self._padding @padding.setter def padding(self, value): self._padding = value @pyqtProperty(QColor) def shadowColor(self): return self._shadowColor @shadowColor.setter def shadowColor(self, color): self._shadowColor = QColor(color)