如何在 PyQt4 中拥有动画系统托盘图标? [英] How can I have an animated system tray icon in PyQt4?

查看:57
本文介绍了如何在 PyQt4 中拥有动画系统托盘图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 pyqt4 应用程序创建动画系统托盘图标,但在 Python 中找不到任何示例.这是我能找到的最接近的,但它是在 C++ 中,我不知道如何翻译它:有没有办法用 pyqt 将(动画)GIF 图像作为系统托盘图标?

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?

如何使用动画 GIF 或使用一系列静止图像作为帧来执行此操作?

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

推荐答案

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

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.

免责声明,仅在 Linux 上测试.

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

这篇关于如何在 PyQt4 中拥有动画系统托盘图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆