优化网速显示

This commit is contained in:
liyp 2024-01-07 16:52:31 +08:00
parent 96518ebc5d
commit 24e70a4615
2 changed files with 18 additions and 49 deletions

View file

@ -17,9 +17,9 @@ class TrayIconExample(QMainWindow):
self.init_ui() self.init_ui()
# 启动异步任务 # 启动异步任务
self.worker_thread = WorkerThread(self) self.traffic_thread = TrafficThread(self)
self.worker_thread.data_ready.connect(self.update_speed_data) self.traffic_thread.data_ready.connect(self.update_speed_data)
self.worker_thread.start() self.traffic_thread.start()
def init_ui(self): def init_ui(self):
self.setWindowTitle('Clash Tray') self.setWindowTitle('Clash Tray')
@ -45,10 +45,6 @@ class TrayIconExample(QMainWindow):
self.download_speed.setCheckable(False) self.download_speed.setCheckable(False)
self.download_speed.setEnabled(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.version=self.get_version()
self.show_version = QAction('内核版本:'+self.version, self) self.show_version = QAction('内核版本:'+self.version, self)
@ -181,58 +177,30 @@ class TrayIconExample(QMainWindow):
# print(env_var) # print(env_var)
clipboard.setText(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): def update_speed_data(self,data):
print(data) print(data)
upload=data['up'] upload=data['up']
down=data['down'] 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): def exit_application(self):
QApplication.quit() QApplication.quit()
# async def fetch_and_output_data(self): class TrafficThread(QThread):
# 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):
data_ready = pyqtSignal(dict) data_ready = pyqtSignal(dict)
def run(self): def run(self):

View file

@ -1,2 +1,3 @@
PyQt5 PyQt5
requests requests
aiohttp