I would like to create a system tray icon on a desktop to check the netwrok connectivity of a PC. What I want to do is that. Keep pinging to an IP address. If destination IP have reponse. Show a green icon. If destination IP did not have a reponse. Show a red Icon. We would not keep ping to the destination IP , but will run the python exe per 10 minutes.
#import platform
#Import module for the ping process
import subprocess
import time
import schedule
#Import module for the system tray icon
import sys
from PyQt5.QtWidgets import QApplication,QSystemTrayIcon,QMenu
from PyQt5.QtGui import QIcon
def showSystemTrayIcon(status):
app=QApplication(sys.argv)
OnlinetrayIcon = QSystemTrayIcon(QIcon("Desktop/Project/Project/System Tray Icon/Online.PNG"),parent=app)
OfflinetrayIcon = QSystemTrayIcon(QIcon("Desktop/Project/Project/System Tray Icon/Offline.PNG"),parent=app)
match status:
case 'Online':
OfflinetrayIcon.hide()
OnlinetrayIcon.show()
print("Show Tray Icon")
sys.exit(app.exec_())
case 'Offline':
OnlinetrayIcon.hide()
OfflinetrayIcon.show()
print("Hide Tray Icon")
sys.exit(app.exec_())
#Set variable
host='8.8.8.8'
Schedule_Second=10
Number_Of_Ping='-4'
#Function to keep ping an IP address
def myping(host):
parameter = Number_Of_Ping
command = ['ping', host, parameter]
response = subprocess.call(command)
print(response)
#If connected
if response == 0:
print('Connected',host)
showSystemTrayIcon('Online')
return True
#If have loss
else:
print('Disconnected',host)
showSystemTrayIcon('Offline')
return False
#Set to run on a schedule basis
schedule.every(Schedule_Second).seconds.do(myping,host=host)
while True:
schedule.run_pending()
But the issue is that: 1.) The scipt need to wait for the system tray icon disappear to continous the ping loop. (Any one knows the duration of the icon show?) 2.) The system tray icon could not keep show on the tool bar.
The behaviou is not what I expect. What I would like to do is that. 1.) Ping the destination IP for every 10 minutes. Show the system tray icon according to the result. 2.) The system tray icon stays in the tool bar until any update. 3.) If the next ping , the reponse is difference , update the icon to a different one.