简体   繁体   中英

How do I integrate an animated GIF in a QMessageBox?

I would like to have a QMessageBox with a moving GIF as icon. So I made this:

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()

where the GIF is :

在此处输入图片说明

But the result is a fixed image. How can I make it animated? I tried something with the QMovie class but I wasn't able to set it in the QMessageBox.setIconPixmap function

QMovie must be used, but for this the first thing is to access the QLabel directly for it, use 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

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