From 24e70a461597936d34e4187feebc46b1127f1f89 Mon Sep 17 00:00:00 2001 From: liyp Date: Sun, 7 Jan 2024 16:52:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BD=91=E9=80=9F=E6=98=BE?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/main.py | 64 ++++++++++++------------------------------------ requirements.txt | 3 ++- 2 files changed, 18 insertions(+), 49 deletions(-) diff --git a/main/main.py b/main/main.py index 1e16ad2..52e03b3 100644 --- a/main/main.py +++ b/main/main.py @@ -17,9 +17,9 @@ class TrayIconExample(QMainWindow): self.init_ui() # 启动异步任务 - self.worker_thread = WorkerThread(self) - self.worker_thread.data_ready.connect(self.update_speed_data) - self.worker_thread.start() + self.traffic_thread = TrafficThread(self) + self.traffic_thread.data_ready.connect(self.update_speed_data) + self.traffic_thread.start() def init_ui(self): self.setWindowTitle('Clash Tray') @@ -45,10 +45,6 @@ class TrayIconExample(QMainWindow): self.download_speed.setCheckable(False) self.download_speed.setEnabled(False) - # self.timer = QTimer(self) - # self.timer.timeout.connect(self.update_data) - # self.timer.start(1000) # 定时器间隔为1000毫秒(1秒) - # 更多 子菜单项设置 self.version=self.get_version() self.show_version = QAction('内核版本:'+self.version, self) @@ -181,58 +177,30 @@ class TrayIconExample(QMainWindow): # print(env_var) clipboard.setText(env_var) + @staticmethod + def convert_speed(speed): + units = ['B/s', 'KB/s', 'MB/s', 'GB/s'] + index = 0 + while speed >= 1024 and index < len(units) - 1: + speed /= 1024.0 + index += 1 + return f'{speed:.2f} {units[index]}' def update_speed_data(self,data): print(data) upload=data['up'] down=data['down'] - + converted_upload_speed = self.convert_speed(upload) + converted_download_speed = self.convert_speed(down) + self.upload_speed.setText(f'↑ {converted_upload_speed}') + self.download_speed.setText(f'↓ {converted_download_speed}') - if upload /1024<=1024 or down /1024<=1024: - upload=round(upload/1024,2) - down=round(down/1024,2) - self.upload_speed.setText('上传速度:'+str(upload)+'KB/s') - self.download_speed.setText('下载速度:'+str(down)+'KB/s') - else: - upload=round(upload/1024/1024,2) - down=round(down/1024/1024,2) - self.upload_speed.setText('上传速度:'+str(upload)+'MB/s') - self.download_speed.setText('下载速度:'+str(down)+'MB/s') - pass def exit_application(self): QApplication.quit() - # async def fetch_and_output_data(self): - # url = "http://127.0.0.1:9090/traffic" # 替换为实际的 API 地址 - # async with aiohttp.ClientSession() as session: - # async with session.get(url, timeout=None) as response: # timeout=None 表示不设置超时 - # while True: - # partial_data = await response.content.read(100) # 每次读取100字节的数据示例 - - # if not partial_data: - # break # 如果没有更多数据,退出循环 - - # # 在此处可以进行异步加载部分数据的操作,例如使用 Qt 的异步加载机制 - # # print("Partial data:", partial_data) - - # # 将字节数据转换为字符串 - # partial_data_str = partial_data.decode('utf-8') - - # print(partial_data_str) - - # # 将字符串解析为 JSON 对象 - # try: - # partial_data_json = json.loads(partial_data_str) - # print("Partial data:", partial_data_json) - # self.speed_data.emit(partial_data_json) - # except json.JSONDecodeError as e: - # print(f"Error decoding JSON: {e}") - - # await asyncio.sleep(1) # 休眠1秒,模拟每秒输出一部分数据 - -class WorkerThread(QThread): +class TrafficThread(QThread): data_ready = pyqtSignal(dict) def run(self): diff --git a/requirements.txt b/requirements.txt index c811f4c..361f221 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ PyQt5 -requests \ No newline at end of file +requests +aiohttp \ No newline at end of file