From 4216dd4ddaf77064503c26c991efd39c0f25ca0f Mon Sep 17 00:00:00 2001 From: liyp Date: Sun, 7 Jan 2024 18:52:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=89=93=E5=8C=85=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9B=E5=B0=8F?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +++- main.spec | 38 ++++++++++++++++++++++++++++++++++++++ main/main.py | 18 +++++++++++++++--- readme.md | 4 +++- requirements.txt | 3 ++- 5 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 main.spec diff --git a/.gitignore b/.gitignore index c12e0a4..8e3984c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .venv main/test.py -main/test1.py \ No newline at end of file +main/test1.py +build +dist \ No newline at end of file diff --git a/main.spec b/main.spec new file mode 100644 index 0000000..f7ef570 --- /dev/null +++ b/main.spec @@ -0,0 +1,38 @@ +# -*- mode: python ; coding: utf-8 -*- + + +a = Analysis( + ['main/main.py'], + pathex=[], + binaries=[], + datas=[('img/*.png', 'img/')], + hiddenimports=[], + hookspath=[], + hooksconfig={}, + runtime_hooks=[], + excludes=[], + noarchive=False, +) +pyz = PYZ(a.pure) + +exe = EXE( + pyz, + a.scripts, + a.binaries, + a.datas, + [], + name='ClashTray', + debug=False, + bootloader_ignore_signals=False, + strip=False, + upx=True, + upx_exclude=[], + runtime_tmpdir=None, + console=True, + disable_windowed_traceback=False, + argv_emulation=False, + target_arch=None, + codesign_identity=None, + entitlements_file=None, + icon='img/icon.png' +) diff --git a/main/main.py b/main/main.py index a637bb8..718e7aa 100644 --- a/main/main.py +++ b/main/main.py @@ -1,4 +1,4 @@ -import sys +import sys,os import platform import requests from PyQt5.QtWidgets import QApplication, QMainWindow, QSystemTrayIcon, QMenu, QAction,QActionGroup @@ -26,7 +26,11 @@ class TrayIconExample(QMainWindow): self.setWindowTitle('Clash Tray') # 创建系统托盘图标 - tray_icon = QSystemTrayIcon(QIcon('img/icon.png'), self) + script_dir = os.path.dirname(os.path.realpath('__file__')) + print(script_dir) + # 构建图标文件的路径 + icon_path = os.path.join(script_dir, 'img', 'icon.png') + tray_icon = QSystemTrayIcon(QIcon(icon_path), self) tray_icon.setToolTip('Clash Tray') # 创建右键菜单 @@ -223,6 +227,11 @@ class TrayIconExample(QMainWindow): self.inuse_memory.setText(f'内存占用:{convert_memory}') def exit_application(self): + # 退出前关闭线程 + if self.traffic_thread.isRunning(): + # self.traffic_thread.terminate() + self.traffic_thread.stop() + self.traffic_thread.wait() QApplication.quit() @@ -237,10 +246,11 @@ class TrafficThread(QThread): self.fetch_json_data("http://127.0.0.1:9090/memory",self.memory_data))) async def fetch_json_data(self,url,signal): + self.should_stop = False # 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: + while not self.should_stop: partial_data = await response.content.read(100) if not partial_data: @@ -254,6 +264,8 @@ class TrafficThread(QThread): except json.JSONDecodeError as e: print(f"解析 JSON 时出错:{e}") await asyncio.sleep(1) + def stop(self): + self.should_stop = True if __name__ == '__main__': app = QApplication(sys.argv) diff --git a/readme.md b/readme.md index ca69c55..e9c76e9 100644 --- a/readme.md +++ b/readme.md @@ -6,4 +6,6 @@ - 设置 Clash 代理模式 - 重启 Clash 核心 - 查看内存占用 -- 查看当前上传下载网速 \ No newline at end of file +- 查看当前上传下载网速 + +打包成单文件:使用 `Pyinstaller` ,包含了 `main.spec` ,使用 `pyinstaller ./main.spec` 即可打包为一个单文件,更多自定义可以编辑 `main.spec` 文件实现 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 361f221..10daaab 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ PyQt5 requests -aiohttp \ No newline at end of file +aiohttp +pyinstaller \ No newline at end of file