• 属性动画QPropertyAnimation---QAbstractAnimation基本功能


    属性动画QPropertyAnimation

    继承于 QAbstractAnimation-->QVariantAnimation-->QPropertyAnimation  

    改变大小、颜色或位置是动画中的常见操作,而QPropertyAnimation类可以修改控件的属性值

    from PyQt5.QtWidgets import QApplication, QWidget,QPushButton
    import sys
    from PyQt5.QtCore import QPropertyAnimation,QPoint,QSize,QRect,QEasingCurve
    
    class win(QWidget):
        def __init__(self):
            super().__init__()
            self.resize(400,400)
            self.setWindowTitle('动画学习')
    
            btn=QPushButton('按钮',self)
            btn.move(100,100)
            btn.resize(100,100)
            btn.setStyleSheet('background-color:yellow')
            #ani=QPropertyAnimation(btn,b'pos',self)  #创建动画对象
            ani = QPropertyAnimation(self)  #创建动画对象
            ani.setTargetObject(btn)  #设置动画目标对象
            #ani.setTargetObject(self)
            ani.setPropertyName(b'pos')   #设置动画属性
            #注意:字节类型
            #pos---位置动画---QPoint
            #size---大小动画---QSize
            #geometry----位置+大小动画----QRect
            #windowOpacity---窗口的透明度(0.0是透明的    1.0是不透明)---好像只适合顶层窗口
    
            ani.setStartValue(QPoint(0,0))  #设置开始位置---按钮的左上角位置
            ani.setEndValue(QPoint(300,300))  #设置结束位置
            #ani.setStartValue(QSize(0, 0))  # 设置开始大小
            #ani.setEndValue(QSize(300, 300))  # 设置结束大小
            #ani.setStartValue(QRect(0, 0,100,100))  # 设置开始位置和大小
            #ani.setEndValue(QRect(100,100,300, 300))  # 设置结束位置和大小
    
            #ani.setStartValue(1)  # 设置开始不透明
            #ani.setKeyValueAt(0.5,0.2)#在动画的某时间点插入一个值
            #参数1 0.0到1.0  0.0表示开始点,1.0表示结束点
            #在动画的中间插入透明度0.2
            #ani.setKeyValueAt(1, 1)  #在动画的结束点是不透明的
    
            #ani.setEndValue(0)  # 设置结束透明
            ani.setDuration(5000)  #设置动画单次时长---单位毫秒
    
            ani.setEasingCurve(QEasingCurve.InQuad)  #设置动画的节奏
            #取值   https://doc.qt.io/qt-5/qeasingcurve.html#Type-enum
    
            ani.start()  #动画开始---非阻塞
    
    if __name__=='__main__':
        app=QApplication(sys.argv)
        w=win()
        w.show()
        sys.exit(app.exec_())

    属性动画的父类功能 

    from PyQt5.QtWidgets import QApplication, QWidget,QPushButton
    import sys
    from PyQt5.QtCore import QPropertyAnimation,QPoint,QSize,QRect,QEasingCurve,QAbstractAnimation
    
    class win(QWidget):
        def __init__(self):
            super().__init__()
            self.resize(400,400)
            self.setWindowTitle('动画学习')
    
            btn=QPushButton('按钮',self)
            btn.move(100,100)
            btn.resize(100,100)
            btn.setStyleSheet('background-color:yellow')
            btn.clicked.connect(self.AA)
            btn1 = QPushButton('暂停', self)
            btn1.clicked.connect(self.BB)
            btn1.move(10,150)
            btn2 = QPushButton('恢复暂停', self)
            btn2.clicked.connect(self.CC)
            btn2.move(10, 200)
            btn3 = QPushButton('停止', self)
            btn3.clicked.connect(self.DD)
            btn3.move(10, 250)
            btn4 = QPushButton('设置时间', self)
            btn4.clicked.connect(self.EE)
            btn4.move(10, 300)
    
    
    
            ani = QPropertyAnimation(self)
            self.ani=ani
            ani.setTargetObject(btn)
            ani.setPropertyName(b'pos')
            ani.setStartValue(QPoint(0,0))
            ani.setEndValue(QPoint(300,300))
            ani.setDuration(5000)
            ani.setEasingCurve(QEasingCurve.InQuad)
            ani.setLoopCount(3)  #设置动画次数---默认1次
            ani.setDirection(QAbstractAnimation.Forward)  #设置动画方向
            #QAbstractAnimation.Backward=1  动画的当前时间随着时间减少(即,从结束/持续时间向0移动)---倒序
            #QAbstractAnimation.Forward=0 动画的当前时间随着时间而增加(即,从0移动到结束/持续时间)---顺序
    
            #信号
            ani.currentLoopChanged.connect(self.FF)  #循环遍数发生变化时
            #会向槽函数传递一个参数---当前循环的遍数
    
            #directionChanged(QAbstractAnimation.Direction newDirection)   动画方向发生改变时
            #会向槽函数传递一个参数---动画新方向
    
            ani.finished.connect(self.HH)  #动画完成时
    
            ani.stateChanged.connect(self.GG)  #状态发生改变时
            # 会向槽函数传递两个参数---新状态和老状态
    
            ani.start() #启动动画
            #参数 QAbstractAnimation.KeepWhenStopped  停止时不会删除动画
            #     QAbstractAnimation.DeleteWhenStopped   停止时动画将自动删除
    
        def AA(self):
            s=self.ani.loopCount()  #返回动画总循环次数
            print('动画总循环次数',s)
            s = self.ani.currentLoop()  # 返回当前是循环中的第几次---0次开始
            #如果setDirection设置为倒序,返回值也是倒序
            print('当前次数是:', s)
            s = self.ani.duration()  #返回单次时长
            print('单次时长是:', s)
            s = self.ani.currentLoopTime()  # 当前单次循环内的时间
            # 如果setDirection设置为倒序,返回值也是倒序
            print('当前单次循环内的时间是:', s)
            s = self.ani.currentTime()  #当前总循环中时长
            #如果setDirection设置为倒序,返回值也是倒序
            print('当前总循环内时长是:', s)
            s=self.ani.direction()  #返回动画方向
            # 返回值 int
            #1  表示倒序;0  表示 顺序
            print('动画方向:',s)
    
        def BB(self):
            #self.ani.pause()   #暂停
            self.ani.setPaused(True)  #是否暂停
            s=self.ani.State()  #返回动画的状态
            #QAbstractAnimation.Paused=1  暂停状态
            #QAbstractAnimation.Stopped=0   停止状态
            #QAbstractAnimation.Running=2   运行状态
            print(s)
    
        def CC(self):
            #self.ani.resume()   #恢复暂停--继续
            self.ani.setPaused(False)  # 是否暂停
            s = self.ani.State()  # 返回动画的状态
            print(s)
    
        def DD(self):
            self.ani.stop()   #停止
    
        def EE(self):
            self.ani.setCurrentTime(14000)  #设置当前动画时间--按总时长计算--毫秒
    
        def FF(self,n):
            print('当前循环遍数是:',n)
    
        def HH(self):
            print('动画播放完毕')
    
        def GG(self,z,z1):
            print('新状态是:',z)
            print('老状态是:',z1)
    
    
    if __name__=='__main__':
        app=QApplication(sys.argv)
        w=win()
        w.show()
        sys.exit(app.exec_())

    天子骄龙

  • 相关阅读:
    slice()与splice()
    apply和call函数
    参数arguments
    获取用户当前位置并设为中心点
    数组中元素为对象形式的去重
    判断浏览器环境(微信、支付宝)
    h5车牌号输入键盘
    点击事件的延迟
    jQuery修改伪元素
    webSocket认识
  • 原文地址:https://www.cnblogs.com/liming19680104/p/10422261.html
Copyright © 2020-2023  润新知