diff --git a/gui/components/simple_tabs.py b/gui/components/simple_tabs.py index 8286df8..52dfab9 100644 --- a/gui/components/simple_tabs.py +++ b/gui/components/simple_tabs.py @@ -7,14 +7,18 @@ 抽出 ``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, X -from typing import Any, Tuple +from tkinter.constants import BOTH, LEFT, X +from typing import Any, Callable, Tuple def setup_simple_text_tab( @@ -34,3 +38,31 @@ def setup_simple_text_tab( 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 diff --git a/gui/gui_app.py b/gui/gui_app.py index b1ad188..9050241 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -67,7 +67,7 @@ from gui.components.filter_tab import setup_filter_tab from gui.components.realtime_tab import setup_realtime_tab from gui.components.win_record_tab import setup_win_record_tab from gui.components.actions_log_toolbar import setup_actions_log_toolbar -from gui.components.simple_tabs import setup_simple_text_tab +from gui.components.simple_tabs import setup_simple_text_tab, setup_stats_tab from gui.components.preview_tab import setup_preview_tab from gui.components.keywords_tab import setup_keywords_tab from gui.components.main_notebook import setup_main_notebook @@ -351,25 +351,7 @@ def main(): status_setter=status_var.set, ) # 统计信息显示 - stats_top, stats_text = setup_simple_text_tab(stats_tab, ScrolledText_cls=ScrolledText) - - def refresh_stats(): - """委托给 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: - import traceback - - 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) + refresh_stats = setup_stats_tab(stats_tab, runner=runner, ScrolledText_cls=ScrolledText) # 预约队列显示 reservation_top, reservation_text = setup_simple_text_tab(reservation_tab, ScrolledText_cls=ScrolledText)