当前位置: 动力学知识库 > 问答 > 编程问答 >

python - PyQt5 - how do I change systray icon (when clicked) before running other code

问题描述:

I am very new to PyQt, so please bear with me.

Here is example code of the sort of thing I have been working on:

#!/usr/bin/python3

# -*- coding: utf-8 -*-

import sys

from PyQt5.QtGui import QIcon

from PyQt5.QtWidgets import (QAction, QApplication,

QMenu,

QSystemTrayIcon,

)

def count():

for x in range(0, 200000):

print("Test " + str(x))

class TrayIcon(QSystemTrayIcon):

def __init__(self):

QSystemTrayIcon.__init__(self)

self.setIcon(QIcon('off.png'))

self.trayIconMenu = QMenu()

item = QAction('Count', self, triggered=self.counter)

self.trayIconMenu.addAction(item)

self.setContextMenu(self.trayIconMenu)

self.show()

def counter(self):

self.setIcon(QIcon('on.png'))

count()

self.setIcon(QIcon('off.png'))

if __name__ == '__main__':

app = QApplication(sys.argv)

trayicon = TrayIcon()

sys.exit(app.exec_())

I would have thought when 'Count' systray menu item is clicked, the systray icon when would change, count() function would do prints and then systray icon would change back. At the moment, I get no appreciable icon change (but the count() function does run). If I take out the function and the second setIcon() statement, then the icon does change.

Any help would be very much appreciated.

分享给朋友:
您可能感兴趣的文章:
随机阅读: