from __future__ import annotations import os import tkinter as tk from pathlib import Path from tkinter import ttk from typing import Iterable, Sequence from . import theme def clear(frame: tk.Misc) -> None: for child in frame.winfo_children(): child.destroy() def card(parent, title: str | None = None, *, padding: int = 14) -> tk.Frame: frame = tk.Frame(parent, bg=theme.SURFACE, highlightbackground=theme.BORDER, highlightthickness=1) if title: ttk.Label(frame, text=title, style="H2.TLabel").pack(anchor="w", padx=padding, pady=(padding, 6)) return frame def metric(parent, title: str, value: object, hint: str = "", color: str = theme.TEAL) -> tk.Frame: box = card(parent) top = tk.Frame(box, bg=theme.SURFACE) top.pack(fill="x", padx=14, pady=(12, 4)) tk.Label(top, text="●", fg=color, bg=theme.SURFACE, font=("Segoe UI", 11, "bold")).pack(side="left") ttk.Label(top, text=title, style="Small.TLabel").pack(side="left", padx=(6, 0)) ttk.Label(box, text=str(value), style="Value.TLabel").pack(anchor="w", padx=14) ttk.Label(box, text=hint, style="Small.TLabel").pack(anchor="w", padx=14, pady=(0, 12)) return box def section(parent, title: str, subtitle: str = "") -> tk.Frame: row = tk.Frame(parent, bg=theme.BG) row.pack(fill="x", pady=(0, 10)) ttk.Label(row, text=title, style="Title.TLabel").pack(side="left") if subtitle: ttk.Label(row, text=subtitle, background=theme.BG, foreground=theme.MUTED, font=theme.FONT_SMALL).pack(side="left", padx=(10, 0)) return row def flow_grid(parent, columns: int = 4) -> tk.Frame: grid = tk.Frame(parent, bg=theme.BG) grid.pack(fill="x", pady=(0, 14)) for idx in range(columns): grid.grid_columnconfigure(idx, weight=1, uniform="metric") return grid def text_panel(parent, title: str, lines: Iterable[str], height: int = 10) -> tk.Frame: box = card(parent, title) text = tk.Text(box, height=height, wrap="none", bd=0, bg="#fbfcfa", fg=theme.TEXT, insertbackground=theme.TEAL, font=theme.FONT_MONO) text.pack(fill="both", expand=True, padx=12, pady=(4, 12)) content = "\n".join(list(lines)[:260]) or "暂无数据" text.insert("1.0", content) text.configure(state="disabled") return box def table_panel(parent, title: str, columns: Sequence[tuple[str, str, int]], rows: Sequence[dict], height: int = 8) -> tk.Frame: box = card(parent, title) tree = ttk.Treeview(box, columns=[c[0] for c in columns], show="headings", height=height) for key, label, width in columns: tree.heading(key, text=label) tree.column(key, width=width, anchor="w") for row in rows: tree.insert("", "end", values=[str(row.get(key, "-")) for key, _, _ in columns]) tree.pack(fill="both", expand=True, padx=12, pady=(4, 12)) return box def field(parent, label: str, value: object, *, wide: bool = False) -> tk.Frame: row = tk.Frame(parent, bg=theme.SURFACE) ttk.Label(row, text=label, style="Small.TLabel", width=18).pack(side="left") ent = ttk.Entry(row) ent.insert(0, str(value)) ent.pack(side="left", fill="x", expand=True) if not wide: ent.configure(width=18) return row def switch_row(parent, label: str, value: bool) -> tk.Frame: row = tk.Frame(parent, bg=theme.SURFACE) var = tk.BooleanVar(value=bool(value)) ttk.Checkbutton(row, text=label, variable=var).pack(side="left") return row def preview_panel(parent, image_path: Path | None, title: str = "截图预览") -> tk.Frame: box = card(parent, title) body = tk.Frame(box, bg="#fbfcfa", highlightbackground=theme.BORDER, highlightthickness=1) body.pack(fill="both", expand=True, padx=12, pady=(4, 12)) if image_path and image_path.exists(): try: from PIL import Image, ImageTk img = Image.open(image_path).convert("RGB") img.thumbnail((320, 520)) photo = ImageTk.PhotoImage(img) label = tk.Label(body, image=photo, bg="#fbfcfa") label.image = photo label.pack(expand=True, pady=10) ttk.Label(body, text=os.path.basename(str(image_path)), background="#fbfcfa", foreground=theme.MUTED).pack(pady=(0, 8)) return box except Exception: pass tk.Label(body, text="暂无截图\n点击“截图+OCR”后刷新", bg="#fbfcfa", fg=theme.MUTED, font=("Microsoft YaHei UI", 14)).pack(expand=True) return box class ScrollFrame(ttk.Frame): def __init__(self, parent): super().__init__(parent, style="Dashboard.TFrame") self.canvas = tk.Canvas(self, bg=theme.BG, highlightthickness=0, bd=0) self.scroll = ttk.Scrollbar(self, orient="vertical", command=self.canvas.yview) self.inner = tk.Frame(self.canvas, bg=theme.BG) self.window_id = self.canvas.create_window((0, 0), window=self.inner, anchor="nw") self.canvas.configure(yscrollcommand=self.scroll.set) self.canvas.pack(side="left", fill="both", expand=True) self.scroll.pack(side="right", fill="y") self.inner.bind("", lambda _e: self.canvas.configure(scrollregion=self.canvas.bbox("all"))) self.canvas.bind("", lambda e: self.canvas.itemconfigure(self.window_id, width=e.width)) self.canvas.bind_all("", self._wheel) def _wheel(self, event) -> None: try: self.canvas.yview_scroll(int(-1 * (event.delta / 120)), "units") except Exception: pass