From c5df7a05d291ca394348cebe4a7cf851ed12aafb Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 30 May 2026 14:30:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor(gui):=20=E6=8A=BD=20update=5Focr=5Fres?= =?UTF-8?q?ults=20=E5=88=B0=20components/ocr=5Fresults=5Fview=20(4148=20?= =?UTF-8?q?=E2=86=92=204117)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gui/components/ocr_results_view.py | 48 ++++++++++++++++++++++++++++++ gui/gui_app.py | 35 ++-------------------- 2 files changed, 50 insertions(+), 33 deletions(-) create mode 100644 gui/components/ocr_results_view.py diff --git a/gui/components/ocr_results_view.py b/gui/components/ocr_results_view.py new file mode 100644 index 0000000..7e4fd6f --- /dev/null +++ b/gui/components/ocr_results_view.py @@ -0,0 +1,48 @@ +"""OCR 结果展示 Tab 的更新逻辑。 + +从 ``gui/gui_app.py`` 抽出。原本是 ``main()`` 内的闭包 ``update_ocr_results``, +被 ``do_screenshot`` 在 OCR 完成后通过 ``root.after`` 调用。 + +依赖通过显式参数注入:``text_widget`` (展示用的 ScrolledText/Text widget)。 +""" + +from __future__ import annotations + +import tkinter as tk +from typing import Sequence, Tuple + + +def update_ocr_results(text_widget: tk.Text, results: Sequence[Tuple[str, str, str, str]]) -> None: + """更新OCR结果标签页的显示""" + try: + lines = [] + lines.append("=" * 80) + lines.append("OCR识别结果(基于最新截图)") + lines.append("=" * 80) + lines.append("") + + if not results: + lines.append('暂无OCR结果,请先点击"截图+OCR"按钮') + else: + # 按区域名称分组显示 + for region_name, desc, text_result, status in results: + lines.append(f"【{region_name}】{desc}") + lines.append(f" 状态: {status}") + if text_result: + # 如果文本较长,分行显示 + if len(text_result) > 100: + lines.append(f" 识别文本: {text_result[:100]}...") + lines.append(f" (完整文本: {text_result})") + else: + lines.append(f" 识别文本: {text_result}") + else: + lines.append(" 识别文本: (空)") + lines.append("") + + text_widget.delete("1.0", "end") + text_widget.insert("1.0", "\n".join(lines)) + text_widget.see("1.0") + except Exception as e: + text_widget.delete("1.0", "end") + text_widget.insert("1.0", f"更新OCR结果失败:{e}") + diff --git a/gui/gui_app.py b/gui/gui_app.py index 72af5f3..dea1424 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -56,6 +56,7 @@ from gui.components.actions_log_view import ( ) from gui.components.wifi_adb_pick_dialog import open_scan_pick_dialog as _open_scan_pick_dialog_impl from gui.components.wifi_adb_reconnect import try_reconnect_last_wifi as _try_reconnect_last_wifi_impl +from gui.components.ocr_results_view import update_ocr_results as _update_ocr_results_impl # 导入日志颜色配置模块 try: @@ -3740,39 +3741,7 @@ def main(): pass def update_ocr_results(results): - """更新OCR结果标签页的显示""" - try: - lines = [] - lines.append("=" * 80) - lines.append("OCR识别结果(基于最新截图)") - lines.append("=" * 80) - lines.append("") - - if not results: - lines.append('暂无OCR结果,请先点击"截图+OCR"按钮') - else: - # 按区域名称分组显示 - for region_name, desc, text_result, status in results: - lines.append(f"【{region_name}】{desc}") - lines.append(f" 状态: {status}") - if text_result: - # 如果文本较长,分行显示 - if len(text_result) > 100: - lines.append(f" 识别文本: {text_result[:100]}...") - lines.append(f" (完整文本: {text_result})") - else: - lines.append(f" 识别文本: {text_result}") - else: - lines.append(" 识别文本: (空)") - lines.append("") - - ocr_results_text.delete("1.0", "end") - ocr_results_text.insert("1.0", "\n".join(lines)) - ocr_results_text.see("1.0") - except Exception as e: - ocr_results_text.delete("1.0", "end") - ocr_results_text.insert("1.0", f"更新OCR结果失败:{e}") - + _update_ocr_results_impl(ocr_results_text, results) ttk.Button( ocr_results_top, text="刷新", command=lambda: update_ocr_results([]) ).pack(side=LEFT)