add trayicon

This commit is contained in:
jxxghp 2023-10-10 13:57:56 +08:00
parent 48dcc3ee1b
commit 3f83894dc6
2 changed files with 31 additions and 5 deletions

View File

@ -1,6 +1,7 @@
import multiprocessing
from pathlib import Path
import pystray
import uvicorn as uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
@ -29,10 +30,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()))
def init_routers():
"""
@ -74,6 +71,14 @@ def stop_frontend():
subprocess.Popen(f"killall nginx", shell=True)
def open_web():
"""
调用浏览器打开前端页面
"""
import webbrowser
webbrowser.open(f"http://{settings.HOST}:{settings.PORT}")
@App.on_event("shutdown")
def shutdown_server():
"""
@ -116,6 +121,26 @@ def start_module():
start_frontend()
# uvicorn服务
Server = uvicorn.Server(Config(App, host=settings.HOST, port=settings.PORT,
reload=settings.DEV, workers=multiprocessing.cpu_count()))
# 托盘图标
TrayIcon = pystray.Icon(
settings.PROJECT_NAME,
icon=settings.ROOT_PATH / 'app.ico',
menu=pystray.Menu(
pystray.MenuItem(
'打开',
open_web,
),
pystray.MenuItem(
'退出',
shutdown_server,
)
)
)
if __name__ == '__main__':
# 初始化数据库
init_db()

View File

@ -53,4 +53,5 @@ requests_cache~=0.5.2
parse~=1.19.0
docker~=6.1.3
cachetools~=5.3.1
fast-bencode==1.1.3
fast-bencode~=1.1.3
pystray~=0.19.5