祝各位道友念头通达
GitHub Gitee 语雀 打赏

pysid6 为组件添加动画

pyside6 为组件添加动画示例

示例1 修改 geometry 属性

# -*- coding: utf-8 -*-
# coding:unicode_escape
from asyncio import sleep
import sys, os, time
from PySide6 import QtCore, QtWidgets, QtGui, QtStateMachine

class Example(QtWidgets.QWidget):

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

        self.initUI()

    def initUI(self):

        self.button = QtWidgets.QPushButton("Start", self)
        self.button.clicked.connect(self.doAnim)
        self.button.move(30, 30)

        self.frame = QtWidgets.QFrame(self)
        self.frame.setFrameStyle(QtWidgets.QFrame.Panel | QtWidgets.QFrame.Raised)
        self.frame.setGeometry(150, 30, 100, 100)

        self.setGeometry(300, 300, 380, 300)
        self.setWindowTitle('Animation')
        self.show()        

    def doAnim(self):

        self.anim = QtCore.QPropertyAnimation(self.frame, b"geometry")
        self.anim.setDuration(10000)
        self.anim.setStartValue(QtCore.QRect(150, 30, 100, 100))
        self.anim.setEndValue(QtCore.QRect(150, 30, 200, 200))
        self.anim.start()

if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    ex = Example()
    ex.show()
    app.exec_()

示例2 动画修改长度, 可以实现进度条

# -*- coding: utf-8 -*-
# coding:unicode_escape
from asyncio import sleep
import sys, os, time
from PySide6 import QtCore, QtWidgets, QtGui, QtStateMachine

class Example(QtWidgets.QWidget):

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

        self.initUI()

    def initUI(self):

        self.button = QtWidgets.QPushButton("Start", self)
        self.button.clicked.connect(self.doAnim)
        self.button.move(30, 30)

        self.frame = QtWidgets.QWidget(self)
        self.frame.setStyleSheet("background-color: red")
        self.frame.resize(30, 50)

        self.resize(200, 200)
        self.setWindowTitle('Animation')
        self.show()        

    def doAnim(self):

        self.anim = QtCore.QPropertyAnimation(self.frame, b"size")
        self.anim.setDuration(1000)
        self.anim.setStartValue(QtCore.QSize(30,50))
        self.anim.setEndValue(QtCore.QSize(100,50))
        self.anim.start()

if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    ex = Example()
    ex.show()
    app.exec_()

示例3 实现进度条

import re
import sys, os, time, math
from PySide6 import QtCore, QtWidgets, QtGui
class LoadWidget(QtWidgets.QDialog):
    def __init__(self, endObj):
        super().__init__()
        self.endObj = endObj
        self.main_layout = QtWidgets.QVBoxLayout(self)
        # 内容居左
        self.setStyleSheet("background-color: #fff;")
        self.testWidght = QtWidgets.QWidget()
        self.testWidght.setStyleSheet("background-color: #fffff0;")
        self.testWidght_layout = QtWidgets.QVBoxLayout(self.testWidght)
        self.testWidght_layout.setContentsMargins(0, 0, 0, 0)
        # self.testWidght_layout.addWidget(QtWidgets.QLabel("正在升级, 请勿点击"))
        self.prosse_layout = QtWidgets.QHBoxLayout()
        # QtWidgets.QPushButton(self.testWidght,"测试")
        # 设置大小
        self.testWidght.setFixedSize(480, 30)
        self.prosse_layout.addWidget(self.testWidght)
        
        
        self.title_lable = QtWidgets.QLabel("正在升级,请稍后...")
        # 设置字体大小
        self.title_lable.setFont(QtGui.QFont("黑体", 10))
        # 设置高度
        self.title_lable.setFixedHeight(25)
        # 设置下边距
        self.title_lable.setContentsMargins(0, 0, 0, 10)
        # 居中显示
        self.main_layout.setAlignment(QtCore.Qt.AlignCenter)
        self.main_layout.addWidget(self.title_lable)
        self.main_layout.addLayout(self.prosse_layout)
        
        self.speval = 0
        self.prosse_len = 480
        self.resize(520, 90)
        # # 设定不可更改大小
        self.setFixedSize(self.width(), self.height())
        # # 不显示标题栏
        self.setWindowFlags(QtCore.Qt.CustomizeWindowHint|QtCore.Qt.WindowStaysOnTopHint)
        self.set_pross(0)
        self.show()
        
    def set_pross(self, speval):
        if(speval == 0):
            # 创建一个进度条
            # self.prosse = QtWidgets.QLabel()
            self.prosse = QtWidgets.QPushButton(self.testWidght,"测试")
            self.prosse.resize(30, 0)
            # 设置背景色
            self.prosse.setStyleSheet("background-color: rgba(133, 122, 144, 0.5);")
            self.pross_animation = QtCore.QPropertyAnimation(self.prosse, b'size')
            self.pross_animation.finished.connect(self.finished_animation)
            self.pross_height = 30
            self.pross_status = QtCore.QSize(0, self.pross_height)
    
        self.pross_num = math.ceil(self.prosse_len * speval); 
        if(self.pross_num > self.prosse_len):
            self.pross_num = 490
            
        self.pross_animation.setStartValue(self.pross_status)
        self.pross_status = QtCore.QSize(self.pross_num, 30)
        self.pross_animation.setEndValue(self.pross_status)
        self.pross_animation.setDuration(1000)
        self.pross_animation.setEasingCurve(QtCore.QEasingCurve.OutSine)
        self.pross_animation.start()
        self.speval = speval
        
    def finished_animation(self):
        self.prosse.setStyleSheet("background-color: green;")
        if(self.speval < 1):
            # todo 获取相应的数据,传值给进度条
            # if self.speval > 0.3:
            #     self.set_pross(0.5)
            # else:
            #     self.set_pross(self.speval + 0.1)
            self.set_pross(self.speval + 0.1)
        
        else:
            print("动画执行完毕")
            self.prosse.setStyleSheet("background-color: rgba(255, 255, 255, 0);")
            self.close()
            
    def closeEvent(self, event):
        print("--------------> pross end1")
        
class MyWidget_test(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.right_layout = QtWidgets.QVBoxLayout()
        self.button = QtWidgets.QPushButton("开始升级")
        self.button1 = QtWidgets.QPushButton("取消升级")
        self.right_layout.addWidget(self.button)
        self.right_layout.addWidget(self.button1)
        
        self.button.clicked.connect(self.click_button)
        self.button1.clicked.connect(self.click_button1)
        
        # 创建子窗口
        main_widght = QtWidgets.QWidget(self)
        main_widght.setLayout(self.right_layout)
        
        self.setCentralWidget(main_widght)
        self.resize(700, 500)
        self.setWindowOpacity(0.9)   #只有设置透明度不为1的时候才会穿透
        self.show()

    def click_button1(self):
        self.loadWidget.close()
        self.loadWidget.destroy()

    def click_button(self):
        self.loadWidget = LoadWidget(self)

    def closeEvent(self, event):
        print("--------------> pross end2")
        self.loadWidget.close()
if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    widget = MyWidget_test()
    widget.setWindowTitle("升级")
    sys.exit(app.exec())
posted @ 2022-03-07 12:11  韩若明瞳  阅读(1134)  评论(0编辑  收藏  举报