我怎样才能在 PyQt4 中有一个动画系统托盘图标?
How can I have an animated system tray icon in PyQt4?
我正在尝试为 pyqt4 应用创建动画系统托盘图标,但在 python 中找不到任何示例。这是我能找到的最接近的,但它是用 C++ 编写的,我不知道如何翻译它:Is there a way to have (animated)GIF image as system tray icon with pyqt?
如何使用动画 GIF 或使用一系列静止图像作为帧来实现这一点?
也许是这样的。创建 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()
我正在尝试为 pyqt4 应用创建动画系统托盘图标,但在 python 中找不到任何示例。这是我能找到的最接近的,但它是用 C++ 编写的,我不知道如何翻译它:Is there a way to have (animated)GIF image as system tray icon with pyqt?
如何使用动画 GIF 或使用一系列静止图像作为帧来实现这一点?
也许是这样的。创建 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()