PyQt - 在其他功能运行时显示 GIF

PyQt - Displaying GIF While Other Function Runs

我有一个将数据保存到文件的应用程序,有时保存功能需要很长时间,因为我的文件很大。我想在保存文件时显示 "loading.gif",但还不知道该怎么做。这是我的代码:

def saveBook(self):
    self.loadingWidget.setHidden(not root.journal.status.loading.isHidden())
    app.processEvents()
    self.excelSheet.save(filename = self.file)
    self.loadingWidget.setHidden(not root.journal.status.loading.isHidden())

LoadingWidget 是持有 GIF(自动开启)的 QLabel。

GIF 已显示,但已暂停。如果我省略第四行,GIF会在保存功能完成后播放,但是我不能让GIF播放保存功能是运行。我该怎么做?

编辑:建议的重复项不正确。那个人试图在标签旁边显示 gif。我试图在运行另一个函数 save 时播放 GIF 帧。 GIF 已经可见,但在当前 save 函数完成之前,应用程序不会处理它的播放。

更新-解决方案:

def saveBook(self):
    #Show GIF
    root.saved.gif.on()
    #Update frame
    GLOB.app.processEvents()
    #Save on separate thread to allow interface to remain active
    t = threading.Thread(target=self.saveThread)
    t.start()

def saveThread(self): #this function runs in background, allowing GIF to run in main thread
    #Prevent multiple threads from queueing to save the file
    if not self.lock.acquire(False):
        return
    self.b.save(self.file)
    #Remove GIF
    root.saved.gif.off()
    self.lock.release()

当 QT 正在处理主事件循环中的其他代码时,GUI 不会更新。通常,当您不想阻塞 GUI 的长 运行 进程时,您会将这些操作推送到单独的线程中。