Displaying gif in PyQt GUI using QLabel

烂漫一生 提交于 2020-01-11 12:17:11

问题


I am trying to display a loading gif after a button is pressed. This is the code I currently have

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MainWindow (QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow,self).__init__(parent)
        self.setGeometry(50,50,240,320)
        self.home()
    def home(self):
        but = QtGui.QPushButton("Example", self)#Creates the brew coffee button
        but.clicked.connect(self.gif_display)
        but.resize(200,80)
        but.move(20,50)

        self.show()
    def gif_display(self):
        l = QMovieLabel('loading.gif')
        l.show()

class QMovieLabel(QLabel):
    def __init__(self, fileName):
        QLabel.__init__(self)
        m = QMovie(fileName)
        m.start()
        self.setMovie(m)

    def setMovie(self, movie):
        QLabel.setMovie(self, movie)
        s=movie.currentImage().size()
        self._movieWidth = s.width()
        self._movieHeight = s.height()

def run():
    app = QtGui.QApplication(sys.argv)
    GUI = MainWindow()
    sys.exit(app.exec_())
run()

I would like to display the gif called "loading.gif" after the button is pressed. Nothing appears after pressing the button and I am unsure of what to do to get the gif to properly appear. The gif is the same size as the screen that I created (240x320).


回答1:


The problem is that QMovieLabel is a local variable within gif_display so it will be deleted when the function finishes running, so the solution is to avoid deleting it. There are 2 options: make it an attribute of the class or make it a child of the window , I will show the second method since I think it is the one you want:

import sys
from PyQt4 import QtCore, QtGui

class MainWindow (QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow,self).__init__(parent)
        self.setGeometry(50,50,240,320)
        self.home()

    def home(self):
        but = QtGui.QPushButton("Example", self) # Creates the brew coffee button
        but.clicked.connect(self.gif_display)
        but.resize(200,80)
        but.move(20,50)
        self.show()

    @QtCore.pyqtSlot()
    def gif_display(self):
        l = QMovieLabel('loading.gif', self)
        l.adjustSize()
        l.show()

class QMovieLabel(QtGui.QLabel):
    def __init__(self, fileName, parent=None):
        super(QMovieLabel, self).__init__(parent)
        m = QtGui.QMovie(fileName)
        self.setMovie(m)
        m.start()

    def setMovie(self, movie):
        super(QMovieLabel, self).setMovie(movie)
        s=movie.currentImage().size()
        self._movieWidth = s.width()
        self._movieHeight = s.height()

def run():
    app = QtGui.QApplication(sys.argv)
    GUI = MainWindow()
    sys.exit(app.exec_())

if __name__ == '__main__':
    run()


来源:https://stackoverflow.com/questions/53134445/displaying-gif-in-pyqt-gui-using-qlabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!