简体   繁体   中英

Can't get PyQt5 to display an animated gif

It was working earlier with layouts, but I can't make it work without, the gif is not showing up.

import os
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QMainWindow
from PyQt5.QtGui import QMovie

scriptDir = os.path.dirname(os.path.realpath(__file__))
gifFile = (scriptDir + os.path.sep + '78.gif')

class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.MovieLabel = QLabel(self)
        self.movie = QMovie(gifFile)
        self.MovieLabel.setMovie(self.movie)
        self.movie.start()

window = MainWindow()
window.show()
sys.exit(app.exec_())

Here's a working rendition of your code. The biggest factor is make sure your QLabel geometry is large enough to accommodate your gif (this also applies for MainWindow ):

import os
import sys
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
from PyQt5.QtGui import QMovie

scriptDir = os.path.dirname(os.path.realpath(__file__))
gifFile = (scriptDir + os.path.sep + '78.gif')


class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        # Resize main window to be 600px / 400px
        self.resize(600, 400)
        self.MovieLabel = QLabel(self)
        # Set gif content to be same size as window (600px / 400px)
        self.MovieLabel.setGeometry(QtCore.QRect(0, 0, 600, 400))
        self.movie = QMovie(gifFile)
        self.MovieLabel.setMovie(self.movie)
        self.movie.start()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM