添加内存占用
This commit is contained in:
parent
24e70a4615
commit
7a4bcd6a1a
1 changed files with 30 additions and 8 deletions
38
main/main.py
38
main/main.py
|
@ -19,6 +19,7 @@ class TrayIconExample(QMainWindow):
|
||||||
|
|
||||||
self.traffic_thread = TrafficThread(self)
|
self.traffic_thread = TrafficThread(self)
|
||||||
self.traffic_thread.data_ready.connect(self.update_speed_data)
|
self.traffic_thread.data_ready.connect(self.update_speed_data)
|
||||||
|
self.traffic_thread.memory_data.connect(self.update_memory_data)
|
||||||
self.traffic_thread.start()
|
self.traffic_thread.start()
|
||||||
|
|
||||||
def init_ui(self):
|
def init_ui(self):
|
||||||
|
@ -45,6 +46,10 @@ class TrayIconExample(QMainWindow):
|
||||||
self.download_speed.setCheckable(False)
|
self.download_speed.setCheckable(False)
|
||||||
self.download_speed.setEnabled(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.version=self.get_version()
|
||||||
self.show_version = QAction('内核版本:'+self.version, self)
|
self.show_version = QAction('内核版本:'+self.version, self)
|
||||||
|
@ -81,6 +86,7 @@ class TrayIconExample(QMainWindow):
|
||||||
context_menu.addMenu(proxy_mode)
|
context_menu.addMenu(proxy_mode)
|
||||||
context_menu.addAction(self.copy_env)
|
context_menu.addAction(self.copy_env)
|
||||||
context_menu.addMenu(self.more_menu)
|
context_menu.addMenu(self.more_menu)
|
||||||
|
context_menu.addAction(self.inuse_memory)
|
||||||
context_menu.addActions([self.upload_speed,self.download_speed,exit_action])
|
context_menu.addActions([self.upload_speed,self.download_speed,exit_action])
|
||||||
proxy_mode.addActions([self.direct_mode,self.rule_mode,self.global_mode])
|
proxy_mode.addActions([self.direct_mode,self.rule_mode,self.global_mode])
|
||||||
|
|
||||||
|
@ -185,6 +191,9 @@ class TrayIconExample(QMainWindow):
|
||||||
speed /= 1024.0
|
speed /= 1024.0
|
||||||
index += 1
|
index += 1
|
||||||
return f'{speed:.2f} {units[index]}'
|
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']
|
||||||
|
@ -194,7 +203,20 @@ class TrayIconExample(QMainWindow):
|
||||||
self.upload_speed.setText(f'↑ {converted_upload_speed}')
|
self.upload_speed.setText(f'↑ {converted_upload_speed}')
|
||||||
self.download_speed.setText(f'↓ {converted_download_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):
|
def exit_application(self):
|
||||||
QApplication.quit()
|
QApplication.quit()
|
||||||
|
@ -202,14 +224,16 @@ class TrayIconExample(QMainWindow):
|
||||||
|
|
||||||
class TrafficThread(QThread):
|
class TrafficThread(QThread):
|
||||||
data_ready = pyqtSignal(dict)
|
data_ready = pyqtSignal(dict)
|
||||||
|
memory_data= pyqtSignal(dict)
|
||||||
def run(self):
|
def run(self):
|
||||||
loop = asyncio.new_event_loop()
|
loop = asyncio.new_event_loop()
|
||||||
asyncio.set_event_loop(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):
|
async def fetch_json_data(self,url,signal):
|
||||||
url = "http://127.0.0.1:9090/traffic"
|
# url = "http://127.0.0.1:9090/traffic"
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get(url, timeout=None) as response:
|
async with session.get(url, timeout=None) as response:
|
||||||
while True:
|
while True:
|
||||||
|
@ -222,13 +246,11 @@ class TrafficThread(QThread):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
partial_data_json = json.loads(partial_data_str)
|
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:
|
except json.JSONDecodeError as e:
|
||||||
print(f"解析 JSON 时出错:{e}")
|
print(f"解析 JSON 时出错:{e}")
|
||||||
|
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app = QApplication(sys.argv)
|
app = QApplication(sys.argv)
|
||||||
window = TrayIconExample()
|
window = TrayIconExample()
|
||||||
|
|
Loading…
Reference in a new issue