From 3f83894dc64d051e1576bb4b5d69f97a44cfe416 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Tue, 10 Oct 2023 13:57:56 +0800 Subject: [PATCH] add trayicon --- app/main.py | 33 +++++++++++++++++++++++++++++---- requirements.txt | 3 ++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index b4ca34cc..123541b8 100644 --- a/app/main.py +++ b/app/main.py @@ -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() diff --git a/requirements.txt b/requirements.txt index 9a00f2b1..f8ba5640 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 \ No newline at end of file +fast-bencode~=1.1.3 +pystray~=0.19.5 \ No newline at end of file