user1616820
user1616820

Reputation: 39

Briefly flash a picture

I'm trying to modify a program written using pyQt (specifically, Anki). I want the program to briefly flash a picture (stored as a file on my hard drive) and then continue running normally.

This code will be inserted at some arbitrary point in the program. This is an ad-hoc single-user patch to an existing program - it does not need to be fast or elegant or easily-maintained.

My problem is that I know very little about pyQt. Do I need to define an entire new "window", or can I just run some sort of "notification" function with an image inside it?

Upvotes: 1

Views: 477

Answers (1)

Avaris
Avaris

Reputation: 36715

QSplashScreen will be useful for this. It is mainly used for displaying certain image/text while a program loads, but your case also looks ideal for this. You can close it with clicking on it, or additionally you can set a timer to auto-close it after some time.

Here is a simple example with a dialog that has one button. When pressed it'll show the image and close after 2 seconds:

import sys
from PyQt4 import QtGui, QtCore

class Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)

        layout = QtGui.QVBoxLayout()
        self.setLayout(layout)

        self.b1 = QtGui.QPushButton('flash splash')
        self.b1.clicked.connect(self.flashSplash)

        layout.addWidget(self.b1)

    def flashSplash(self):
        # Be sure to keep a reference to the SplashScreen
        # otherwise it'll be garbage collected
        # That's why there is 'self.' in front of the name
        self.splash = QtGui.QSplashScreen(QtGui.QPixmap('/path/to/image.jpg'))

        # SplashScreen will be in the center of the screen by default.
        # You can move it to a certain place if you want.
        # self.splash.move(10,10)

        self.splash.show()

        # Close the SplashScreen after 2 secs (2000 ms)
        QtCore.QTimer.singleShot(2000, self.splash.close)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    main = Dialog()
    main.show()

    sys.exit(app.exec_())

Upvotes: 5

Related Questions