PyQt5基础学习-不规则图像动画效果

通过一个定时器实现动画效果,通过转动索引值,设置图片的QPixmap实现定时的效果

AnimationWindows.py 

"""
不规则窗体的动画实现
"""

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPixmap, QPainter, QCursor
from PyQt5.QtCore import Qt, QTimer

class AnimationWindows(QWidget):
    def __init__(self):
        super(AnimationWindows, self).__init__()
        self.i = 1
        self.mypix()
        self.timer = QTimer()
        self.timer.setInterval(500)
        self.timer.timeout.connect(self.timeChange)
        self.timer.start()

    #显示不规则 pic
    def mypix(self):
        self.update()
        if self.i == 5:
            self.i = 1
        self.mypic = {1: '../picture/left.png', 2: "../picture/up.png", 3: '../picture/right.png', 4: '../picture/down.png'}
        self.pix = QPixmap(self.mypic[self.i])
        self.resize(self.pix.size())
        self.setMask(self.pix.mask())
        self.dragPosition = None

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.m_drag = True
            self.m_DragPosition = event.globalPos() - self.pos()

            self.setCursor(QCursor(Qt.OpenHandCursor))

    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_drag:
            self.move(QMouseEvent.globalPos() - self.m_DragPosition)


    def mouseReleaseEvent(self, QMouseEvent):
        self.m_drag = False
        self.setCursor(QCursor(Qt.ArrowCursor))

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(0, 0, self.pix.width(), self.pix.height(), self.pix)

    #鼠标双击事件,
    def mouseDoubleClickEvent(self, event):
        if event.button() == 1:
            self.i += 1
            self.mypix()

    #每500毫秒修改paint
    def timeChange(self):
        self.i += 1
        self.mypix()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = AnimationWindows()
    form.show()
    sys.exit(app.exec_())

 

posted @ 2022-02-13 13:41  c语言我的最爱  阅读(250)  评论(0编辑  收藏  举报