添加打包方式,修复一些小问题

This commit is contained in:
liyp 2024-01-07 18:52:09 +08:00
parent 16eba9aa5d
commit 4216dd4dda
5 changed files with 61 additions and 6 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
.venv .venv
main/test.py main/test.py
main/test1.py main/test1.py
build
dist

38
main.spec Normal file
View file

@ -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'
)

View file

@ -1,4 +1,4 @@
import sys import sys,os
import platform import platform
import requests import requests
from PyQt5.QtWidgets import QApplication, QMainWindow, QSystemTrayIcon, QMenu, QAction,QActionGroup from PyQt5.QtWidgets import QApplication, QMainWindow, QSystemTrayIcon, QMenu, QAction,QActionGroup
@ -26,7 +26,11 @@ class TrayIconExample(QMainWindow):
self.setWindowTitle('Clash Tray') 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') tray_icon.setToolTip('Clash Tray')
# 创建右键菜单 # 创建右键菜单
@ -223,6 +227,11 @@ class TrayIconExample(QMainWindow):
self.inuse_memory.setText(f'内存占用:{convert_memory}') self.inuse_memory.setText(f'内存占用:{convert_memory}')
def exit_application(self): def exit_application(self):
# 退出前关闭线程
if self.traffic_thread.isRunning():
# self.traffic_thread.terminate()
self.traffic_thread.stop()
self.traffic_thread.wait()
QApplication.quit() QApplication.quit()
@ -237,10 +246,11 @@ class TrafficThread(QThread):
self.fetch_json_data("http://127.0.0.1:9090/memory",self.memory_data))) self.fetch_json_data("http://127.0.0.1:9090/memory",self.memory_data)))
async def fetch_json_data(self,url,signal): async def fetch_json_data(self,url,signal):
self.should_stop = False
# 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 not self.should_stop:
partial_data = await response.content.read(100) partial_data = await response.content.read(100)
if not partial_data: if not partial_data:
@ -254,6 +264,8 @@ class TrafficThread(QThread):
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)
def stop(self):
self.should_stop = True
if __name__ == '__main__': if __name__ == '__main__':
app = QApplication(sys.argv) app = QApplication(sys.argv)

View file

@ -7,3 +7,5 @@
- 重启 Clash 核心 - 重启 Clash 核心
- 查看内存占用 - 查看内存占用
- 查看当前上传下载网速 - 查看当前上传下载网速
打包成单文件:使用 `Pyinstaller` ,包含了 `main.spec` ,使用 `pyinstaller ./main.spec` 即可打包为一个单文件,更多自定义可以编辑 `main.spec` 文件实现

View file

@ -1,3 +1,4 @@
PyQt5 PyQt5
requests requests
aiohttp aiohttp
pyinstaller