from __future__ import annotations
import argparse
import ctypes
import html
import sys
import time
from pathlib import Path
from PIL import ImageGrab
ROOT = Path(__file__).resolve().parents[2]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
try:
ctypes.windll.user32.SetProcessDPIAware()
except Exception:
pass
from gui.dashboard_app.app import DashboardApp
from gui.dashboard_app.page_registry import PAGES
def _slug(name: str) -> str:
safe = "".join(ch for ch in name if ch not in '\\/:*?"<>|').strip()
return safe or "page"
def _capture_window(app: DashboardApp, out: Path) -> None:
try:
app.root.attributes("-topmost", True)
app.root.lift()
app.root.focus_force()
except Exception:
pass
app.root.update_idletasks()
app.root.update()
time.sleep(0.2)
app.root.update()
x = app.root.winfo_rootx()
y = app.root.winfo_rooty()
w = app.root.winfo_width()
h = app.root.winfo_height()
ImageGrab.grab(bbox=(x, y, x + w, y + h)).save(out)
try:
app.root.attributes("-topmost", False)
except Exception:
pass
def export(out_dir: Path) -> Path:
out_dir.mkdir(parents=True, exist_ok=True)
app = DashboardApp()
app.root.geometry("1280x860+80+60")
app.root.update()
shots = []
try:
for idx, name in enumerate(PAGES, 1):
app.show_page(name)
app.root.lift()
app.root.update()
filename = f"{idx:02d}-{_slug(name)}.png"
path = out_dir / filename
_capture_window(app, path)
shots.append((name, filename))
finally:
app.root.destroy()
index = out_dir / "index.html"
cards = "\n".join(
f'{html.escape(name)}
共导出 {len(shots)} 个界面,点击图片可看原尺寸。