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