关于python:如何在PyQt4中使用动画系统任务栏图标?

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

我正在尝试为pyqt4应用程序创建一个动画的系统托盘图标,但是在python中找不到任何示例时遇到了麻烦。 这是我能找到的最接近的,但是它是用C ++编写的,我不知道如何将其转换:是否可以通过pyqt将(动画化的)GIF图像用作系统任务栏图标?

我该如何使用动画GIF或通过使用一系列静止图像作为帧来进行此操作?


也许是这样的。 创建AnimatedSystemTrayIcon要使用的QMovie实例。 连接到电影的frameChanged信号,并在QSystemTrayIcon上调用setIcon。 您需要将QMovie.currentPixmap返回的像素图转换为QIcon,以传递给setIcon

免责声明,仅在Linux上进行了测试。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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()