From 728ef3cd79a7c50f339a54bcf36a6da9966a2b1a Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 30 May 2026 16:49:01 +0800 Subject: [PATCH] =?UTF-8?q?refactor(gui):=205=20=E4=B8=AA=E7=AE=80?= =?UTF-8?q?=E5=8D=95=E6=96=87=E6=9C=AC=20Tab=20=E7=94=A8=20simple=5Ftabs?= =?UTF-8?q?=20=E6=A8=A1=E6=9D=BF=E5=A4=8D=E7=94=A8=20(1571=20=E2=86=92=201?= =?UTF-8?q?537)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gui/components/simple_tabs.py | 36 +++++++++++++++++++++++++++ gui/gui_app.py | 46 +++++------------------------------ 2 files changed, 42 insertions(+), 40 deletions(-) create mode 100644 gui/components/simple_tabs.py diff --git a/gui/components/simple_tabs.py b/gui/components/simple_tabs.py new file mode 100644 index 0000000..8286df8 --- /dev/null +++ b/gui/components/simple_tabs.py @@ -0,0 +1,36 @@ +"""通用的"简单文本 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`` 中刷新内容。 +""" + +from __future__ import annotations + +import tkinter as tk +from tkinter import ttk +from tkinter.constants import BOTH, X +from typing import Any, 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 diff --git a/gui/gui_app.py b/gui/gui_app.py index 99ea7a5..a090c79 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -98,6 +98,7 @@ from gui.components.log_text_colors import configure_log_text_colors from gui.components.log_search_toolbar import setup_log_search_toolbar 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 # 导入日志颜色配置模块 try: @@ -1403,14 +1404,7 @@ def main(): status_setter=status_var.set, ) # 统计信息显示 - stats_top = ttk.Frame(stats_tab) - stats_top.pack(fill=X, padx=10, pady=(10, 0)) - stats_text = ScrolledText(stats_tab) - stats_text.pack(fill=BOTH, expand=True, padx=10, pady=10) - try: - stats_text.configure(font=("Consolas", 10)) - except Exception: - pass + stats_top, stats_text = setup_simple_text_tab(stats_tab, ScrolledText_cls=ScrolledText) def refresh_stats(): """委托给 gui/components/stats_formatter.format_stats(纯字符串构建)。""" @@ -1431,14 +1425,7 @@ def main(): ttk.Button(stats_top, text="刷新", command=refresh_stats).pack(side=LEFT) # 预约队列显示 - reservation_top = ttk.Frame(reservation_tab) - reservation_top.pack(fill=X, padx=10, pady=(10, 0)) - reservation_text = ScrolledText(reservation_tab) - reservation_text.pack(fill=BOTH, expand=True, padx=10, pady=10) - try: - reservation_text.configure(font=("Consolas", 10)) - except Exception: - pass + reservation_top, reservation_text = setup_simple_text_tab(reservation_tab, ScrolledText_cls=ScrolledText) def refresh_reservations(): _refresh_reservations_impl(reservation_text) @@ -1448,14 +1435,7 @@ def main(): ) # 高价值房间显示 - high_value_top = ttk.Frame(high_value_tab) - high_value_top.pack(fill=X, padx=10, pady=(10, 0)) - high_value_text = ScrolledText(high_value_tab) - high_value_text.pack(fill=BOTH, expand=True, padx=10, pady=10) - try: - high_value_text.configure(font=("Consolas", 10)) - except Exception: - pass + high_value_top, high_value_text = setup_simple_text_tab(high_value_tab, ScrolledText_cls=ScrolledText) def refresh_high_value_rooms(): _refresh_high_value_rooms_impl(high_value_text) @@ -1465,14 +1445,7 @@ def main(): ) # 行为日志显示(点击/识别/耗时) - actions_log_top = ttk.Frame(actions_log_tab) - actions_log_top.pack(fill=X, padx=10, pady=(10, 0)) - actions_log_text = ScrolledText(actions_log_tab) - actions_log_text.pack(fill=BOTH, expand=True, padx=10, pady=10) - try: - actions_log_text.configure(font=("Consolas", 10)) - except Exception: - pass + actions_log_top, actions_log_text = setup_simple_text_tab(actions_log_tab, ScrolledText_cls=ScrolledText) _actions_log_handles = setup_actions_log_toolbar( actions_log_top, @@ -1482,14 +1455,7 @@ def main(): refresh_actions_log = _actions_log_handles.refresh_actions_log # OCR结果显示 - ocr_results_top = ttk.Frame(ocr_results_tab) - ocr_results_top.pack(fill=X, padx=10, pady=(10, 0)) - ocr_results_text = ScrolledText(ocr_results_tab) - ocr_results_text.pack(fill=BOTH, expand=True, padx=10, pady=10) - try: - ocr_results_text.configure(font=("Consolas", 10)) - except Exception: - pass + ocr_results_top, ocr_results_text = setup_simple_text_tab(ocr_results_tab, ScrolledText_cls=ScrolledText) def update_ocr_results(results): _update_ocr_results_impl(ocr_results_text, results)