Robin Fauser
Robin Fauser

Reputation: 121

PYQT5 GIF freezes on GUI initialization

Keep gif running while GUI starts. Is that possible? I have read many reporitys but none with the true and understandable answer.

I have prepared a code example that shows the problem.

Own Gif

import sys
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5 import QtWidgets
from PyQt5.QtGui import QMovie
from PyQt5.QtCore import QSize, QThread
class Main_Window(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(500, 500))
        self.setWindowTitle("Main Window")

        centralWidget = QWidget(self)
        self.setCentralWidget(centralWidget)

        gridLayout = QGridLayout(self)
        centralWidget.setLayout(gridLayout)

        gif = QLabel(self)
        gif.setGeometry(0,0,500,500)
        self.movie = QMovie(r"C:\Users\...\Pictures\Icon_LOAD.gif")
        gif.setMovie(self.movie)
        self.movie.start()


        #   #Call event handler to process the queue, but it is shocking, dirty and unsuitable
        #app.processEvents()
        self.show()

        for i in range(0,1000000):
            print(i)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWin = Main_Window()
    sys.exit(app.exec_())

Upvotes: 1

Views: 430

Answers (1)

nxtgen_boy
nxtgen_boy

Reputation: 11

Now it works smooth

import sys
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5 import QtWidgets
from PyQt5.QtGui import QMovie
from PyQt5.QtCore import QSize
from threading import Thread

class Main_Window(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(500, 500))
        self.setWindowTitle("Main Window")

        centralWidget = QWidget(self)
        self.setCentralWidget(centralWidget)

        gridLayout = QGridLayout(self)
        centralWidget.setLayout(gridLayout)

        gif = QLabel(self)
        gif.setGeometry(0,0,500,500)
        self.movie = QMovie(r"gif.gif")
        gif.setMovie(self.movie)
        self.movie.start()

        self.show()

        Thread(target=self.function_on_thread).start()

    def function_on_thread(self):
        for i in range(0,1000000):
            print(i)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWin = Main_Window()
    sys.exit(app.exec_())

Upvotes: 1

Related Questions