How can I have an animated system tray icon in PyQt4?

1.6k views Asked by At

I am trying to create an animated systray icon for a pyqt4 app but am having trouble finding any examples in python. This is the closest I can find but it's in C++ and I don't know how to translate it over: Is there a way to have (animated)GIF image as system tray icon with pyqt?

How can I go about doing this either with an animated GIF or by using a series of still images as frames?

1

There are 1 answers

1
Paul Rooney On BEST ANSWER

Maybe something like this. Create QMovie instance to be used by AnimatedSystemTrayIcon. Connect to the frameChanged signal of the movie and call setIcon on the QSystemTrayIcon. You need to convert the pixmap returned by QMovie.currentPixmap to a QIcon to pass to setIcon.

Disclaimer, only tested on Linux.

import sys
from PyQt4 import QtGui

class AnimatedSystemTrayIcon(QtGui.QSystemTrayIcon):

    def UpdateIcon(self):
        icon = QtGui.QIcon()
        icon.addPixmap(self.iconMovie.currentPixmap())
        self.setIcon(icon)

    def __init__(self, movie, parent=None):
        super(AnimatedSystemTrayIcon, self).__init__(parent)
        menu = QtGui.QMenu(parent)
        exitAction = menu.addAction("Exit")
        self.setContextMenu(menu)

        self.iconMovie = movie
        self.iconMovie.start()

        self.iconMovie.frameChanged.connect(self.UpdateIcon)

def main():
    app = QtGui.QApplication(sys.argv)

    w = QtGui.QWidget()
    trayIcon = AnimatedSystemTrayIcon(movie=QtGui.QMovie("cat.gif"), parent=w)

    w.resize(250, 150)
    w.move(300, 300)
    w.setWindowTitle('Anim Systray')
    w.show()

    trayIcon.show()

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()