托盘图标应用程序的线程

threading for tray icon application

我想要一个托盘图标来通知我我的 COM 端口是否已插入。它应该根据 COM 端口的状态每 5 秒更改一次。我还希望能够使用托盘图标的上下文菜单终止程序。我想出了如何获得刷新或菜单,但我不知道如何同时拥有两者。

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()  

感谢无花果,发现使用 QTimer,似乎是重复的,并不理解所有内容,但它有效:

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)