PyQt:在其他操作运行时显示动画 GIF
我有一个加载 GIF,我想将其显示为加载屏幕,直到特定方法完成。我的问题是 GIF 不会继续运行,因为还有另一个函数在工作,并且 GIF 应该继续运行直到该函数完成。
import sys
import time
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QMovie
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
def wait_till_function_done():
time.sleep(5)
class Window(QMainWindow):
def __init__(self):
super().__init__()
class Splash:
def showSplash(self):
label = QLabel()
label.setWindowFlag(Qt.SplashScreen)
# label for displaying GIF
self.movie = QMovie(".../Loading.gif")
label.setMovie(self.movie)
self.movie.start()
label.show()
# -------
# Simulate the operation that is talking about.
# The GIF should keep working and playing till the following simulation is done.
# -------
# But The GIF stop running and that is because the following line block Qt event
wait_function_is_done() # this line simulate this sentence: "keep showing loading screen till the operation is done"
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = Window()
splash_obj = Splash()
# start the Splash screen
splash_obj.showSplash()
# when it's done, it will be closed and main_app showen
main_window.show()
app.exec_()
我尝试将 QThread 与加载 gif 一起使用,因此它将在该方法/操作运行时更改 gif 帧(并且应该在该函数完成时关闭)。但这并没有成功:
- 框架没有改变。 (我可以使用 app.processEvents() 以便可以更改框架)。
- 它被困在 while 循环中。
class GIFThread(QThread):
signal = pyqtSignal()
def __init__(self):
super(GIFThread, self).__init__()
class Splash:
def showSplash(self):
.....
# init thread
thread = GIFThread()
thread.signal.connect(self.change_gif_frame)
thread.start()
# start changing GIF frames
thread.signal.emit()
# -------
# Simulate the operation.
time.sleep(5)
# this should stop the thread and break the while loop
thread.quit()
def change_gif_frame(self):
while True:
self.movie.jumpToNextFrame()
I have a loading GIF, and I want to show it as a loading screen till a specific method is finished. My problem is that the GIF won't keep running because there is another function working, and that GIF should keep running till that function is done.
import sys
import time
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QMovie
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
def wait_till_function_done():
time.sleep(5)
class Window(QMainWindow):
def __init__(self):
super().__init__()
class Splash:
def showSplash(self):
label = QLabel()
label.setWindowFlag(Qt.SplashScreen)
# label for displaying GIF
self.movie = QMovie(".../Loading.gif")
label.setMovie(self.movie)
self.movie.start()
label.show()
# -------
# Simulate the operation that is talking about.
# The GIF should keep working and playing till the following simulation is done.
# -------
# But The GIF stop running and that is because the following line block Qt event
wait_function_is_done() # this line simulate this sentence: "keep showing loading screen till the operation is done"
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = Window()
splash_obj = Splash()
# start the Splash screen
splash_obj.showSplash()
# when it's done, it will be closed and main_app showen
main_window.show()
app.exec_()
I've tried to use QThread
with the loading gif, so it will change the gif frames while that method/operation is running (and should be closed when that function is done). But that didn't work out:
- The frames are not changing. (I can use
app.processEvents()
so the frames can be changed). - It is stuck in the while loop.
class GIFThread(QThread):
signal = pyqtSignal()
def __init__(self):
super(GIFThread, self).__init__()
class Splash:
def showSplash(self):
.....
# init thread
thread = GIFThread()
thread.signal.connect(self.change_gif_frame)
thread.start()
# start changing GIF frames
thread.signal.emit()
# -------
# Simulate the operation.
time.sleep(5)
# this should stop the thread and break the while loop
thread.quit()
def change_gif_frame(self):
while True:
self.movie.jumpToNextFrame()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好使用 QSplashScreen ,并使用 QThread 在后台运行应用程序初始化。如有必要,可以通过自定义信号使用进度消息更新启动屏幕。这是一个基本演示,展示了如何实现它:
It's probably best to use QSplashScreen for this, and use QThread to run the app initialisation in the background. If necessary, the splash-screen can be updated with progress messages via a custom signal. Here is a basic demo that shows how to implement that: