Files
douyi_guaji_main/tools/gui/export_dashboard_ui_review.py
T
2026-06-18 12:51:03 +08:00

113 lines
3.1 KiB
Python

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'<section><h2>{html.escape(name)}</h2><a href="{html.escape(filename)}"><img src="{html.escape(filename)}" alt="{html.escape(name)}"></a></section>'
for name, filename in shots
)
index.write_text(
f"""<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>gui_app 新 UI 核对包</title>
<style>
body {{ margin: 0; padding: 28px; background: #f5f7f4; color: #172322; font-family: "Microsoft YaHei UI", sans-serif; }}
h1 {{ margin: 0 0 8px; font-size: 24px; }}
p {{ margin: 0 0 24px; color: #6f7f7d; }}
main {{ display: grid; grid-template-columns: repeat(auto-fit, minmax(460px, 1fr)); gap: 18px; }}
section {{ background: white; border: 1px solid #dbe5df; padding: 14px; }}
h2 {{ margin: 0 0 10px; font-size: 15px; }}
img {{ display: block; width: 100%; height: auto; border: 1px solid #dbe5df; }}
</style>
</head>
<body>
<h1>gui_app 新 UI 核对包</h1>
<p>共导出 {len(shots)} 个界面,点击图片可看原尺寸。</p>
<main>{cards}</main>
</body>
</html>
""",
encoding="utf-8",
)
return index
def main() -> None:
parser = argparse.ArgumentParser(description="导出 gui_app 新 UI 的全页面截图核对包")
parser.add_argument("--out", default="artifacts/gui_dashboard_ui_review", help="输出目录")
args = parser.parse_args()
index = export(Path(args.out).resolve())
print(index)
if __name__ == "__main__":
main()