托盘图标应用程序的穿线 [英] threading for tray icon application

查看:90
本文介绍了托盘图标应用程序的穿线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个托盘图标来通知我我的COM端口是否已插入.它应根据COM端口的状态每5秒更改一次.我还希望能够使用任务栏图标的上下文菜单杀死程序.我想出了如何具有刷新或菜单的功能,但我不知道如何同时具有这两种功能.

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

推荐答案

由于使用了无花果,因此使用QTimer进行了发现,它似乎是重复的,虽然不了解所有内容,但仍然有效:

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)      

这篇关于托盘图标应用程序的穿线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆