From 4c9a66f586c373ed89079cf00a6f880bdcb337a5 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Tue, 10 Oct 2023 14:44:27 +0800 Subject: [PATCH] fix trayicon --- .github/workflows/build.yml | 4 ++- app/main.py | 58 +++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c01f6df3..64649737 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -91,8 +91,10 @@ jobs: Remove-Item -Path "dist.zip" Remove-Item -Path "dist" -Recurse -Force Move-Item -Path "nginx/html/nginx.conf" -Destination "nginx/conf/nginx.conf" -Force - New-Item -Path "nginx/temp" -ItemType Directory -Force + New-Item -Path "nginx/temp" -ItemType Directory -Force + New-Item -Path "nginx/temp/__keep__.txt" -ItemType File -Force New-Item -Path "nginx/logs" -ItemType Directory -Force + New-Item -Path "nginx/logs/__keep__.txt" -ItemType File -Force shell: pwsh - name: Pyinstaller diff --git a/app/main.py b/app/main.py index 58331e42..a190ef93 100644 --- a/app/main.py +++ b/app/main.py @@ -32,7 +32,6 @@ App.add_middleware( allow_headers=["*"], ) - # uvicorn服务 Server = uvicorn.Server(Config(App, host=settings.HOST, port=settings.PORT, reload=settings.DEV, workers=multiprocessing.cpu_count())) @@ -120,37 +119,46 @@ def start_module(): start_frontend() -def open_web(): +def start_tray(): """ - 调用浏览器打开前端页面 + 启动托盘图标 """ - import webbrowser - webbrowser.open(f"http://localhost:{settings.NGINX_PORT}") + if not SystemUtils.is_frozen(): + return -def quit_app(): - """ - 退出程序 - """ - TrayIcon.stop() - Server.should_exit = True + def open_web(): + """ + 调用浏览器打开前端页面 + """ + import webbrowser + webbrowser.open(f"http://localhost:{settings.NGINX_PORT}") + def quit_app(): + """ + 退出程序 + """ + TrayIcon.stop() + Server.should_exit = True -# 托盘图标 -TrayIcon = pystray.Icon( - settings.PROJECT_NAME, - icon=Image.open(settings.ROOT_PATH / 'app.ico'), - menu=pystray.Menu( - pystray.MenuItem( - '打开', - open_web, - ), - pystray.MenuItem( - '退出', - quit_app, + # 托盘图标 + TrayIcon = pystray.Icon( + settings.PROJECT_NAME, + icon=Image.open(settings.ROOT_PATH / 'app.ico'), + menu=pystray.Menu( + pystray.MenuItem( + '打开', + open_web, + ), + pystray.MenuItem( + '退出', + quit_app, + ) ) ) -) + # 启动托盘图标 + threading.Thread(target=TrayIcon.run, daemon=True).start() + if __name__ == '__main__': # 初始化数据库 @@ -158,6 +166,6 @@ if __name__ == '__main__': # 更新数据库 update_db() # 启动托盘 - threading.Thread(target=TrayIcon.run, daemon=True).start() + start_tray() # 启动API服务 Server.run()