1- from app .common .RouterInfo import Router_HW
2- # from PyQt5.QtGui import *
3- # from PyQt5.QtWidgets import *
4-
51import os
62import sys
3+ import asyncio
74
85from PyQt5 .QtCore import Qt , QTranslator , QTimer , QTime
96from PyQt5 .QtGui import QFont , QIcon
10- from PyQt5 .QtWidgets import QApplication , QSystemTrayIcon , QMenu , QAction
7+ from PyQt5 .QtWidgets import QSystemTrayIcon , QMenu , QAction
118from qfluentwidgets import FluentTranslator , DWMMenu
9+ from qasync import QEventLoop , QApplication
1210
1311from app .common .config import cfg
1412from app .view .main_window import MainWindow
1513from app .common .global_logger import logger
14+ from app .common .RouterInfo import Router_HW
1615
1716
1817
5352 def show_window (button ):
5453 if button == QSystemTrayIcon .Trigger :
5554 w .show ()
56- update ()
55+ w .update_monitoring_status ()
56+ # w.update_month_statistics()
5757 else :
5858 pass
5959
@@ -80,28 +80,38 @@ def show_window(button):
8080
8181 w .show ()
8282
83- def update ():
83+ async def update ():
8484 router .update_monitoring_status ()
8585 icon = QIcon (router .get_battery_icon_path ())
8686 tray .setIcon (icon )
8787 if w .isVisible ():
8888 w .update_monitoring_status ()
8989 w .update_month_statistics ()
9090
91- def update_traffic_statistics ():
91+ async def update_traffic_statistics ():
9292 if w .isVisible ():
9393 router .update_traffic_statistics ()
9494 w .update_traffic_statistics ()
9595
96- timer = QTimer ()
97- timer .timeout .connect (update )
98- timer .start (15000 )
96+ async def main ():
97+ while True :
98+ await update ()
99+ await asyncio .sleep (15 ) # Run update every 15 seconds
99100
100- timer_traffic = QTimer ()
101- timer_traffic .timeout .connect (update_traffic_statistics )
102- timer_traffic .start (1000 )
103-
104- app .exec_ ()
101+ async def traffic_statistics_main ():
102+ while True :
103+ await update_traffic_statistics ()
104+ await asyncio .sleep (1 ) # Run update_traffic_statistics every 1 second
105105
106-
106+ # Start the main and traffic statistics tasks
107+ loop = QEventLoop (app )
108+ asyncio .set_event_loop (loop )
109+
110+ loop .create_task (main ())
111+ loop .create_task (traffic_statistics_main ())
112+
113+ # Start the application event loop
114+ with loop :
115+ # loop.run_until_complete(app_close_event.wait())
116+ loop .run_forever ()
107117
0 commit comments