Search code examples
pythonmultithreadingpyqt4system-traytrayicon

threading for tray icon application


I want to have a tray icon to inform me whether or not my COM port is plugged-in. It should change every 5 seconds according to the state of the COM port. I also want the ability to kill the program using the contextual menu of the tray icon. I figured out how to have the refreshing or the menu, but I don't know how to have both.

import sys
import glob
import serial
import time
from PyQt4 import QtGui, QtCore
import sys
import threading
from multiprocessing import Process, Queue

#script needing python 3.4 , pyserial (via pip) and pyqt4 (via .exe available online)
def serial_ports():

    if sys.platform.startswith('win'):
        ports = ['COM' + str(i + 1) for i in range(256)]


    result = []
    for port in ports:
        try:
            s = serial.Serial(port)
            s.close()
            result.append(port)
        except (OSError, serial.SerialException):
            pass
    return result


class SystemTrayIcon(QtGui.QSystemTrayIcon):

    def __init__(self, icon, parent=None):
        QtGui.QSystemTrayIcon.__init__(self, icon, parent)
        menu = QtGui.QMenu(parent)
        changeicon = menu.addAction("Update")
        exitAction = menu.addAction("Exit")
        self.setContextMenu(menu)
        exitAction.triggered.connect(QtGui.qApp.quit)
        changeicon.triggered.connect(self.updateIcon)


    def updateIcon(self):

        resultats = serial_ports()
        icone = "red.ico"
        for resultat in resultats:
            if "COM3" in resultat:
                icone = "green.ico"
                break

        self.setIcon(QtGui.QIcon(icone))
        #update the icon (its color) according to the content of "resultat"

#missing code; purpose : wait 5 seconds while having the contextual menu of the tray icon still available

def main():
    app = QtGui.QApplication(sys.argv)

    w = QtGui.QWidget()
    trayIcon = SystemTrayIcon(QtGui.QIcon("red.ico"), w)
    #always starts with red icon
    trayIcon.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()  

Solution

  • Found out using QTimer thanks to figs, seems repetitive and didn't understand everything but it works :

    class SystemTrayIcon(QtGui.QSystemTrayIcon):
    
        def __init__(self, icon, parent=None):
            QtGui.QSystemTrayIcon.__init__(self, icon, parent)
            menu = QtGui.QMenu(parent)
            changeicon = menu.addAction("Update")
            exitAction = menu.addAction("Exit")
            self.setContextMenu(menu)
            exitAction.triggered.connect(QtGui.qApp.quit)
            changeicon.triggered.connect(self.updateIcon)
            self.updateIcon()
    
        def updateIcon(self):
            try:
                timer = QtCore.QTimer()
                timer.timeout.connect(self.updateIcon)
                timer.start(5000)
                resultats = serial_ports()
                icone = "red.ico"
                for resultat in resultats:
                    if "COM3" in resultat:
                        icone = "green.ico"
                        break
    
                self.setIcon(QtGui.QIcon(icone))
    
            finally:
                QtCore.QTimer.singleShot(5000,self.updateIcon)