89 lines
2.9 KiB
Python
89 lines
2.9 KiB
Python
"""通用的"简单文本 Tab"工厂(顶部按钮区 + ScrolledText + 可选配置)。
|
|
|
|
从 ``gui/gui_app.py`` 抽出。多个 Tab(统计信息 / 预约队列 / 高价值房间 /
|
|
行为日志 / OCR 结果)用相同模式:
|
|
- top frame
|
|
- ScrolledText 文本主体(Consolas 10 字体)
|
|
|
|
抽出 ``setup_simple_text_tab`` 复用此模板。返回 ``(top_frame, text_widget)``,
|
|
调用方负责接下来在 ``top_frame`` 中加按钮 / 在 ``text_widget`` 中刷新内容。
|
|
|
|
``setup_stats_tab`` 进一步把 stats 专用的 ``refresh`` 闭包内置进来,
|
|
从 ``gui_app.py`` 抽出对应的 try/except 包装。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import tkinter as tk
|
|
import traceback
|
|
from tkinter import ttk
|
|
from tkinter.constants import BOTH, LEFT, X
|
|
from typing import Any, Callable, Tuple
|
|
|
|
|
|
def setup_simple_text_tab(
|
|
tab: tk.Misc,
|
|
*,
|
|
ScrolledText_cls: Any,
|
|
) -> Tuple[ttk.Frame, tk.Text]:
|
|
"""构建 "顶部按钮区 + ScrolledText" 模板,返回 ``(top, text)``。"""
|
|
top = ttk.Frame(tab)
|
|
top.pack(fill=X, padx=10, pady=(10, 0))
|
|
|
|
text = ScrolledText_cls(tab)
|
|
text.pack(fill=BOTH, expand=True, padx=10, pady=10)
|
|
try:
|
|
text.configure(font=("Consolas", 10))
|
|
except Exception:
|
|
pass
|
|
|
|
return top, text
|
|
|
|
|
|
def setup_stats_tab(
|
|
stats_tab: tk.Misc,
|
|
*,
|
|
runner: Any,
|
|
ScrolledText_cls: Any,
|
|
) -> Callable[[], None]:
|
|
"""构建统计信息 Tab + 刷新按钮,返回 ``refresh_stats`` 回调。"""
|
|
stats_top, stats_text = setup_simple_text_tab(stats_tab, ScrolledText_cls=ScrolledText_cls)
|
|
|
|
def refresh_stats() -> None:
|
|
"""委托给 gui/components/stats_formatter.format_stats(纯字符串构建)。"""
|
|
try:
|
|
from gui.components.stats_formatter import format_stats
|
|
|
|
stats = runner.get_stats()
|
|
stats_text.delete("1.0", "end")
|
|
stats_text.insert("1.0", "\n".join(format_stats(stats)))
|
|
except Exception as e:
|
|
stats_text.delete("1.0", "end")
|
|
error_detail = traceback.format_exc()
|
|
stats_text.insert(
|
|
"1.0", f"刷新统计信息失败:{e}\n\n详细信息:\n{error_detail}"
|
|
)
|
|
|
|
ttk.Button(stats_top, text="刷新", command=refresh_stats).pack(side=LEFT)
|
|
return refresh_stats
|
|
|
|
|
|
def setup_text_tab_with_refresh(
|
|
tab: tk.Misc,
|
|
*,
|
|
refresh_impl: Callable[[tk.Text], None],
|
|
ScrolledText_cls: Any,
|
|
) -> Callable[[], None]:
|
|
"""``setup_simple_text_tab`` + 一个绑定到刷新按钮的 ``refresh_impl(text)`` 回调。
|
|
|
|
用于 reservation / high_value / ocr_results 等"刷新按钮 → 把内容写入 text"的简单 Tab。
|
|
返回零参数的 ``refresh()`` 给调用方调度。
|
|
"""
|
|
top, text = setup_simple_text_tab(tab, ScrolledText_cls=ScrolledText_cls)
|
|
|
|
def refresh() -> None:
|
|
refresh_impl(text)
|
|
|
|
ttk.Button(top, text="刷新", command=refresh).pack(side=LEFT)
|
|
return refresh
|