引入的库:
[Python] 纯文本查看 复制代码from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QMessageBox, QDialog, QVBoxLayout
from PyQt5.QtCore import pyqtSignal, QTimer, QThread, QObject, Qt
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QLabel
from PyQt5.QtGui import QMovie, QIcon
定义的gif播放类:
[Python] 纯文本查看 复制代码class WorkerThread(QThread):
finished = pyqtSignal(str)
def __init__(self, task, *args):
super().__init__()
self.task = task
self.args = args
def run(self):
# 执行任务,并传递参数
result = self.task(self.args[0])
self.finished.emit(result)
class GifPlayer(QDialog):
def __init__(self, parent, gif_path, task, *args):
super().__init__(parent, Qt.FramelessWindowHint | Qt.Tool) # 设置窗口标志
self.setAttribute(Qt.WA_TranslucentBackground) # 设置窗口背景透明
# 设置样式表来确保QLabel的背景透明
self.setStyleSheet("QLabel { background-color: transparent; }")
self.setParent(parent)
self.label = QLabel(self)
layout = QVBoxLayout(self)
layout.addWidget(self.label)
self.movie = QMovie(gif_path)
self.label.setMovie(self.movie)
self.label.show()
self.worker_thread = WorkerThread(task, args[0])
self.worker_thread.finished.connect(self.on_task_finished)
def start_animation(self):
self.label.show()
self.movie.start()
self.worker_thread.start() # 开始线程
def stop_animation(self):
self.movie.stop()
def on_task_finished(self, result):
self.movie.stop()
# dosomething....
self.reject()
调用演示:
[Python] 纯文本查看 复制代码gif_player = GifPlayer(self, './waiting/waiting.gif', self.ReadAndSetText, filename) # './waiting/waiting.gif'这个是gif的路径,self.ReadAndSetText这个是我要处理的任务函数
gif_player.start_animation()
gif_player.exec_()
效果展示:
11.gif (165.13 KB, 下载次数: 0)
下载附件
2024-5-26 12:01 上传
欢迎大家交流讨论