PyQt5中QPropertyAnimation的应用

# -*- coding: utf-8 -*-

from PyQt5.Qt import *
import sys
import time
import numpy as np


class BallCartoon(QMainWindow):
    n = 0
    flag = 0
    ani_w = None

    def __init__(self):
        super(BallCartoon, self).__init__()
        self.init_ui()

    def init_ui(self):
        self.resize(1000, 600)
        self.setWindowTitle('弹 弹 弹')
        self.setFixedSize(self.width(), self.height())
        self.btn_start = QPushButton(self)
        self.btn_start.setText('Start')
        self.btn_start.setGeometry(500, 500, 100, 50)

        self.btn_start.clicked.connect(self.slot_btn_start)

    def resizeEvent(self, event):
        palette = QPalette()
        pix = QPixmap('yu.jpg')
        pix = pix.scaled(self.width(), self.height())
        palette.setBrush(QPalette.Background, QBrush(pix))
        self.setPalette(palette)

    def slot_btn_start(self):
        self.mbt = MyBeautifulThread()
        self.mbt.trigger.connect(self.mimi)
        self.mbt.start()

    def mimi(self, msg):
        self.btn_1 = QPushButton(self)
        self.btn_1.setStyleSheet('''
                            QPushButton
                             {text-align : center;
                             background-color : cyan;
                             font: bold;
                             border-color: gray;
                             border-width: 2px;
                             border-radius: 10px;
                             padding: 6px;
                             height : 14px;
                             border-style: outset;
                             font : 14px;}
                             QPushButton:pressed
                             {text-align : center;
                             background-color : red;
                             font: bold;
                             border-color: cyan;
                             border-width: 2px;
                             border-radius: 10px;
                             padding: 6px;
                             height : 14px;
                             border-style: outset;
                             font : 14px;}
                             ''')
        self.btn_1.setGeometry(200, 300, 20, 20)
        self.btn_1.show()
        self.down(self.btn_1, QEasingCurve.OutBounce)
        self.n = np.random.randint(0, 950)

    def down(self, kj, css):
        animation = QPropertyAnimation(kj, b'pos', self)
        animation.setStartValue(QPoint(self.n, 0))
        animation.setEndValue(QPoint(self.n, 500))
        animation.setDuration(2500)
        animation.setEasingCurve(css)
        animation.start()
        animation.finished.connect(kj.deleteLater)

    def closeEvent(self, event):
        if self.ani_w is None:
            self.ani_w = QPropertyAnimation(self, b'windowOpacity', self)
            self.ani_w.setStartValue(1)
            self.ani_w.setEndValue(0)
            self.ani_w.setDuration(3000)
            self.ani_w.finished.connect(self.close)
            self.ani_w.start()
            event.ignore()


class MyBeautifulThread(QThread):
    trigger = pyqtSignal(str)

    def __init__(self):
        super(MyBeautifulThread, self).__init__()

    def run(self):
        w = BallCartoon()
        while 1:
            self.trigger.emit('6')
            time.sleep(0.2)


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

效果:

 

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值