PyQT5 (九十八)使用计时器Timer 实现异形窗口的动画效果 案例

PyQt5 实现异形窗口的动画效果 案例
自己准备4张png图片作为关键帧图片

素材:Black.png

 

代码:

import sys

from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QIcon, QBitmap, QPainter, QPixmap, QCursor
from PyQt5.QtWidgets import QHBoxLayout, QPushButton, QMessageBox, QApplication, QVBoxLayout, QWidget, \
    QLabel, QGridLayout, QLineEdit, QTextEdit, QFormLayout, QComboBox

'''
PyQt5 实现异形窗口的动画效果 案例
自己准备4张png图片作为关键帧图片

'''


class AnimationWindowDemo(QWidget):

    def __init__(self,parent = None):
        super(AnimationWindowDemo,self).__init__(parent)
        self.initUI()

    def initUI(self):
        # 设置定位和左上角坐标
        self.setGeometry(300, 300, 360, 260)
        # 设置窗口标题
        self.setWindowTitle('异形窗口 的演示')
        # 设置窗口图标
        # self.setWindowIcon(QIcon('../web.ico'))
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
        self.setStyleSheet('''background-color:black; ''')

        self.i = 1
        self.mypix()
        self.timer = QTimer()
        self.timer.setInterval(500) # 执行周期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:'Black.png',
            2:'../web.ico',
            3:'Black.png',
            4:'../web.ico'
        }
        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))

            print(event.globalPos())
            print(event.pos())
            print(self.pos())

        if event.button() == Qt.RightButton:
            self.close()

    def mouseMoveEvent(self,event):
        if Qt.LeftButton and self.m_drag:
            # 当左键移动窗体修改偏移值 QPoint
            # 实时计算窗口左上角坐标
            self.move(event.globalPos() - self.m_DragPosition)

    def mouseReleaseEvent(self,event):
        if 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)
    # 设置应用图标
    app.setWindowIcon(QIcon('../web.ico'))
    w = AnimationWindowDemo()

    w.show()
    sys.exit(app.exec_())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值