diff --git a/main/main.py b/main/main.py index 52e03b3..b228c53 100644 --- a/main/main.py +++ b/main/main.py @@ -19,6 +19,7 @@ class TrayIconExample(QMainWindow): self.traffic_thread = TrafficThread(self) self.traffic_thread.data_ready.connect(self.update_speed_data) + self.traffic_thread.memory_data.connect(self.update_memory_data) self.traffic_thread.start() def init_ui(self): @@ -45,6 +46,10 @@ class TrayIconExample(QMainWindow): self.download_speed.setCheckable(False) self.download_speed.setEnabled(False) + self.inuse_memory=QAction('内存占用',self) + self.inuse_memory.setCheckable(False) + self.inuse_memory.setEnabled(False) + # 更多 子菜单项设置 self.version=self.get_version() self.show_version = QAction('内核版本:'+self.version, self) @@ -81,6 +86,7 @@ class TrayIconExample(QMainWindow): context_menu.addMenu(proxy_mode) context_menu.addAction(self.copy_env) context_menu.addMenu(self.more_menu) + context_menu.addAction(self.inuse_memory) context_menu.addActions([self.upload_speed,self.download_speed,exit_action]) proxy_mode.addActions([self.direct_mode,self.rule_mode,self.global_mode]) @@ -185,6 +191,9 @@ class TrayIconExample(QMainWindow): speed /= 1024.0 index += 1 return f'{speed:.2f} {units[index]}' + + + # 设置网速 def update_speed_data(self,data): print(data) upload=data['up'] @@ -193,8 +202,21 @@ class TrayIconExample(QMainWindow): converted_download_speed = self.convert_speed(down) self.upload_speed.setText(f'↑ {converted_upload_speed}') self.download_speed.setText(f'↓ {converted_download_speed}') - + @staticmethod + def convert_memory(speed): + units = ['B', 'KB', 'MB', 'GB'] + index = 0 + while speed >= 1024 and index < len(units) - 1: + speed /= 1024.0 + index += 1 + return f'{speed:.2f} {units[index]}' + # 设置内存 + def update_memory_data(self,data): + print(data) + memory=data['inuse'] + convert_memory=self.convert_memory(memory) + self.inuse_memory.setText(f'内存占用:{convert_memory}') def exit_application(self): QApplication.quit() @@ -202,14 +224,16 @@ class TrayIconExample(QMainWindow): class TrafficThread(QThread): data_ready = pyqtSignal(dict) - + memory_data= pyqtSignal(dict) def run(self): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) - loop.run_until_complete(self.fetch_and_output_data()) + # 多个协程 + loop.run_until_complete(asyncio.gather(self.fetch_json_data("http://127.0.0.1:9090/traffic",self.data_ready), + self.fetch_json_data("http://127.0.0.1:9090/memory",self.memory_data))) - async def fetch_and_output_data(self): - url = "http://127.0.0.1:9090/traffic" + async def fetch_json_data(self,url,signal): + # url = "http://127.0.0.1:9090/traffic" async with aiohttp.ClientSession() as session: async with session.get(url, timeout=None) as response: while True: @@ -222,13 +246,11 @@ class TrafficThread(QThread): try: partial_data_json = json.loads(partial_data_str) - self.data_ready.emit(partial_data_json) + signal.emit(partial_data_json) except json.JSONDecodeError as e: print(f"解析 JSON 时出错:{e}") - await asyncio.sleep(1) - if __name__ == '__main__': app = QApplication(sys.argv) window = TrayIconExample()