【问题标题】:How can I have an animated system tray icon in PyQt4?如何在 PyQt4 中有一个动画系统托盘图标?
【发布时间】:2015-06-15 01:32:00
【问题描述】:

我正在尝试为 pyqt4 应用程序创建一个动画系统托盘图标,但在 python 中找不到任何示例。这是我能找到的最接近的,但它是用 C++ 编写的,我不知道如何翻译它:Is there a way to have (animated)GIF image as system tray icon with pyqt?

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

【问题讨论】:

    标签: python qt pyqt pyqt4 system-tray


    【解决方案1】:

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

    免责声明,仅在 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()
    

    【讨论】:

    • 感谢您的解决方案。它对我有用。 +1 提问和 +1 回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 2011-06-25
    相关资源
    最近更新 更多