我想要一个托盘图标来通知我我的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()
答案 0 :(得分:0)
由于无花果而使用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)