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

三世轮回 提交于 2019-12-13 14:26:54

问题


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:


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


来源:https://stackoverflow.com/questions/30836292/how-can-i-have-an-animated-system-tray-icon-in-pyqt4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!