打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
pyqt5 动画学习(三) 指定控件的移动轨迹

这一篇来讲解自定义控件的移动轨迹

原理:我们采用QPainterPath先画一个弧线,然后加载一个物体让物体移动,设置100个关键帧,每个关键帧物体的坐标位置就是弧线的坐标位置,这样就能达到按照指定轨迹移动

 

首先,我们重写QLabel,加载一个小黑球到QLabel上

class Ball(QLabel):    def __init__(self, parent):        super(Ball, self).__init__(parent)        self.pix = QPixmap("../ball.png")  # 加载一个ball的图片        self.h = self.pix.height()  # ball的高度        self.w = self.pix.width()  # ball的宽度        self.setPixmap(self.pix)  # 把ball加载到label上    def _set_pos(self, pos):        self.move(pos.x() - self.w / 2, pos.y() - self.h / 2)    pos = pyqtProperty(QPointF, fset=_set_pos)

 

然后我们用QPainterPath来画弧线,先设置弧线:

    def initView(self):        self.path = QPainterPath()        self.path.moveTo(30, 30)        self.path.cubicTo(30, 30, 200, 350, 350, 30)  # 设置弧线的样子

再通过paintEvent绘制弧线:

    def paintEvent(self, e):        qp = QPainter()        qp.begin(self)        qp.setRenderHint(QPainter.Antialiasing)        qp.drawPath(self.path)  # 画弧线        qp.end()

 

最后组合起来就行了,剩下的不难,下面是完整代码:

#!/usr/bin/python3# -*- coding: utf-8 -*-"""PyQt5 Animation tutorialThis program will show along curve with QPropertyAnimation.Author: Semishigure 401219180@qq.comLast edited: 2018.03.02"""from PyQt5.QtWidgets import *from PyQt5.QtGui import *from PyQt5.QtCore import *import sysclass Ball(QLabel):    def __init__(self, parent):        super(Ball, self).__init__(parent)        self.pix = QPixmap("../ball.png")  # 加载一个ball的图片        self.h = self.pix.height()  # ball的高度        self.w = self.pix.width()  # ball的宽度        self.setPixmap(self.pix)  # 把ball加载到label上    def _set_pos(self, pos):        self.move(pos.x() - self.w / 2, pos.y() - self.h / 2)    pos = pyqtProperty(QPointF, fset=_set_pos)class Example(QWidget):    def __init__(self):        super(Example, self).__init__()        self.initView()        self.initAnimation()    def initView(self):        self.path = QPainterPath()        self.path.moveTo(30, 30)        self.path.cubicTo(30, 30, 200, 350, 350, 30)  # 设置弧线的样子        self.ball = Ball(self)        self.ball.pos = QPointF(30, 30)  # 设置ball起点位置,这里就是弧线的起点位置        self.setWindowTitle("Animation along curve")        self.setGeometry(300, 300, 400, 300)        self.show()    def paintEvent(self, e):        qp = QPainter()        qp.begin(self)        qp.setRenderHint(QPainter.Antialiasing)        qp.drawPath(self.path)  # 画弧线        qp.end()    def initAnimation(self):        self.anim = QPropertyAnimation(self.ball, b'pos')        self.anim.setDuration(3000)        self.anim.setStartValue(QPointF(30, 30))        vals = [p / 100 for p in range(0, 101)]        for i in vals:            self.anim.setKeyValueAt(i, self.path.pointAtPercent(i))  # 设置100个关键帧        self.anim.setEndValue(QPointF(350, 30))        self.anim.start()if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

界面预览效果:

 

备注:

1、关键帧的范围值为0-1,我们在其中创建100个点,即[0.01, 0.02, 0.03, 0.04...0.98, 0.99, 1.00],这里直接使用for循环 vals = [p / 100 for p in range(0, 101)]

2、通过i传递百分比到self.path.pointAtPercent()就可以拿到弧线的对应坐标QPoinF,所以关键帧就可以设置成self.anim.setKeyValueAt(i, self.path.pointAtPercent(i))

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
用 eric6 与 PyQt5 实现python的极速GUI编程
PyQt5案例汇总(完整版)
蛇年当然贪吃蛇 PyQt版贪吃蛇引子
Python3实现简单可学习的手写识别
python – QWidget上的QPixmap上的绘图点(pyqt5)
python自制的文件解压缩小工具,同时支持7z/zip/rar三种格式!
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服