From 5f7d93f170ac5fcf4419f3b04f0ebf16a7b9ae58 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Tue, 10 Oct 2023 16:23:57 +0800 Subject: [PATCH] fix startup --- app/main.py | 85 +++++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/app/main.py b/app/main.py index ed05a05b..88aa19c1 100644 --- a/app/main.py +++ b/app/main.py @@ -4,7 +4,6 @@ import sys import threading from pathlib import Path -import pystray import uvicorn as uvicorn from PIL import Image from fastapi import FastAPI @@ -85,6 +84,49 @@ def stop_frontend(): subprocess.Popen(f"killall nginx_mp", shell=True) +def start_tray(): + """ + 启动托盘图标 + """ + + if not SystemUtils.is_frozen(): + return + + def open_web(): + """ + 调用浏览器打开前端页面 + """ + import webbrowser + webbrowser.open(f"http://localhost:{settings.NGINX_PORT}") + + def quit_app(): + """ + 退出程序 + """ + TrayIcon.stop() + Server.should_exit = True + + import pystray + + # 托盘图标 + 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() + + @App.on_event("shutdown") def shutdown_server(): """ @@ -127,47 +169,6 @@ def start_module(): start_frontend() -def start_tray(): - """ - 启动托盘图标 - """ - - if not SystemUtils.is_frozen(): - return - - 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, - ) - ) - ) - # 启动托盘图标 - threading.Thread(target=TrayIcon.run, daemon=True).start() - - if __name__ == '__main__': # 启动托盘 start_tray()