更新WebUI

This commit is contained in:
Eric-Terminal
2025-10-22 13:50:25 +08:00
parent 3cfbf7ed68
commit 043d2c6890
16 changed files with 1567 additions and 101 deletions

54
main.py
View File

@@ -1,9 +1,12 @@
import tkinter as tk
from app_ui import MainApp
from config_manager import ConfigManager
from api_services import ApiService
import sys
import os
import socket
import sys
import threading
import time
import webbrowser
from config_manager import ConfigManager
from web_app import create_app
def get_config_path():
@@ -21,17 +24,40 @@ def get_config_path():
# 开发环境,使用当前目录
return "config.json"
def find_available_port(start: int = 4567, limit: int = 4667) -> int:
"""Return the first free TCP port within the inclusive range."""
for port in range(start, limit + 1):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
sock.bind(("127.0.0.1", port))
return port
except OSError:
continue
raise RuntimeError(f"无法在 {start}-{limit} 范围内找到可用端口")
def open_browser_later(url: str, delay: float = 1.0) -> None:
"""Open the default browser after a small delay."""
def _opener():
time.sleep(delay)
try:
webbrowser.open_new(url)
except Exception:
pass
threading.Thread(target=_opener, daemon=True).start()
if __name__ == "__main__":
# 1. 初始化核心服务
# 使用合适的配置文件路径
config_path = get_config_path()
config_manager = ConfigManager(config_path)
app = create_app(config_manager)
# 2. 创建Tkinter主窗口
root = tk.Tk()
# 3. 实例化主应用服务将在MainApp内部创建
app = MainApp(root, config_manager)
port = find_available_port()
url = f"http://127.0.0.1:{port}/"
print(f"🚀 Web UI 已启动,访问: {url}")
open_browser_later(url)
# 4. 启动Tkinter事件循环
root.mainloop()
app.run(host="127.0.0.1", port=port, debug=False, use_reloader=False)