如何在QMessageBox中集成动画GIF?

时间:2018-04-25 13:39:42

标签: python pyqt icons pyqt5 qmessagebox

我希望QMessageBox有一个移动的GIF作为图标。所以我做到了:

from PyQt5.QtGui import * 
from PyQt5.QtWidgets import *
import os
import sys

def msg_wait(s):
    msg = QMessageBox()
    msg.setIconPixmap(QPixmap('wait.gif').scaledToWidth(100))
    msg.setText(s)
    msg.setWindowTitle(" ")
    msg.setModal(False)
    # msg.setStandardButtons(QMessageBox.Ok)
    msg.show()
    return msg


class SurfViewer(QMainWindow):
    def __init__(self, parent=None):
        super(SurfViewer, self).__init__()
        self.parent = parent
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)

        self.msg=msg_wait('blablabla')
        self.msg.setStandardButtons(QMessageBox.Cancel)

def main():
    app = QApplication(sys.argv)
    ex = SurfViewer(app)
    ex.setWindowTitle('NMM Stimulator')

    ex.showMaximized()
    ex.show()
    # ex.move(0, 0)
    # ex.resizescreen()
    sys.exit(app.exec_( ))


if __name__ == '__main__':
    main()

GIF在哪里:

enter image description here

但结果是固定的图像。 我怎样才能让它变得生动? 我尝试使用QMovie类,但我无法在QMessageBox.setIconPixmap函数中设置它

1 个答案:

答案 0 :(得分:3)

必须使用

QMovie,但为此首先要直接访问QLabel,请使用findChild

def msg_wait(s):
    msg = QMessageBox()
    # create Label
    msg.setIconPixmap(QPixmap('wait.gif').scaledToWidth(100))
    icon_label = msg.findChild(QLabel, "qt_msgboxex_icon_label")
    movie = QMovie('wait.gif')
    # avoid garbage collector
    setattr(msg, 'icon_label', movie)
    icon_label.setMovie(movie)
    movie.start()

    msg.setText(s)
    msg.setWindowTitle(" ")
    msg.setModal(False)
    # msg.setStandardButtons(QMessageBox.Ok)
    msg.show()
    return msg