0

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.

poetofzwan
  • 11
  • 5
  • You're probably running your code in the gui thread, hard to say more without seeing your actual code. You should probably read up on [threading in qt](http://doc.qt.io/qt-5/thread-basics.html)... – mata Aug 31 '15 at 14:58
  • I think threading doesn't help here, at least not alone. I assume you also need to run the event-loop to make Qt aware of the changes to GUI elements. Then process, or run the loop until your worker-thread finishes. But I agree with @mata - code talks. – deets Sep 01 '15 at 09:35
  • I've added an example, I hope that helps clarity my problem :-/ – poetofzwan Sep 05 '15 at 15:45

0 Answers0