QMovie设置文件名第二次不会工作

时间:2015-07-10 21:43:34

标签: qt pyqt

我正在设置初始电影文件:

self.movie = QMovie("dotGreenBlack.gif", QByteArray(), self)

在运行时,我想通过设置来改变它:

self.movie.setFileName("dotGreyStatic.gif")

但是这导致了主要设置文件的框架冻结" dotGreenBlack.gif"

我还需要做些什么才能在运行时更改Gif-Animation?

以下是完整代码:

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

import sys




class ImagePlayer(QWidget):
    def __init__(self, filename, title, parent=None):
        QWidget.__init__(self, parent)

        # Load the file into a QMovie
        self.movie = QMovie(filename, QByteArray(), self)

        size = self.movie.scaledSize()
        self.setGeometry(200, 200, size.width(), size.height())
        self.setWindowTitle(title)

        self.movie_screen = QLabel()
        # Make label fit the gif
        self.movie_screen.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.movie_screen.setAlignment(Qt.AlignCenter)

        # Create the layout
        main_layout = QVBoxLayout()
        main_layout.addWidget(self.movie_screen)



        self.setLayout(main_layout)

        # Add the QMovie object to the label
        self.movie.setCacheMode(QMovie.CacheAll)
        self.movie.setSpeed(100)
        self.movie_screen.setMovie(self.movie)
        self.movie.start()
        self.movie.loopCount()

        self.movie.setFileName("dotGreyStatic.gif")
        self.movie_screen.setMovie(self.movie)



if __name__ == "__main__":
    gif = "dotGreenBlack.gif"
    app = QApplication(sys.argv)
    app.setStyleSheet("QWidget { background-color: black }")
    player = ImagePlayer(gif, "was")
    player.show()
    sys.exit(app.exec_())

此示例中使用的Gif图标:

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

您只需要停止并重新启动电影:

    self.movie.stop()
    self.movie.setFileName("dotGreyStatic.gif")
    self.movie.start()