fix startup

This commit is contained in:
jxxghp 2023-10-10 16:23:57 +08:00
parent 0fbe51f257
commit 5f7d93f170

View File

@ -4,7 +4,6 @@ import sys
import threading import threading
from pathlib import Path from pathlib import Path
import pystray
import uvicorn as uvicorn import uvicorn as uvicorn
from PIL import Image from PIL import Image
from fastapi import FastAPI from fastapi import FastAPI
@ -85,6 +84,49 @@ def stop_frontend():
subprocess.Popen(f"killall nginx_mp", shell=True) 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") @App.on_event("shutdown")
def shutdown_server(): def shutdown_server():
""" """
@ -127,47 +169,6 @@ def start_module():
start_frontend() 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__': if __name__ == '__main__':
# 启动托盘 # 启动托盘
start_tray() start_tray()