From a1562a513dc8f044c110914dd401c18f984dcfe8 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 30 May 2026 17:55:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor(gui):=20=E6=8A=BD=E9=A1=B6=E9=83=A8?= =?UTF-8?q?=E5=B8=B8=E9=A9=BB=E6=8C=89=E9=92=AE=E5=8C=BA=E5=88=B0=20compon?= =?UTF-8?q?ents/top=5Fbuttons=20(527=20=E2=86=92=20518)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gui/components/top_buttons.py | 48 +++++++++++++++++++++++++++++++++++ gui/gui_app.py | 37 ++++++++++----------------- 2 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 gui/components/top_buttons.py diff --git a/gui/components/top_buttons.py b/gui/components/top_buttons.py new file mode 100644 index 0000000..98b8438 --- /dev/null +++ b/gui/components/top_buttons.py @@ -0,0 +1,48 @@ +"""GUI 顶部常驻按钮区(保存配置 + 截图+OCR)。 + +从 ``gui/gui_app.py`` 抽出。原本是 ``main()`` 内长 21 行的 widget 创建段。 +""" + +from __future__ import annotations + +import tkinter as tk +from tkinter import ttk +from tkinter.constants import LEFT, RIGHT +from typing import Callable, Sequence, Tuple + +from gui.components.screenshot_action import do_screenshot as _do_screenshot_impl + + +def setup_top_buttons( + top: tk.Misc, + *, + device_var: tk.StringVar, + y_var: tk.IntVar, + image_var: tk.StringVar, + on_line: Callable[[str], None], + root: tk.Misc, + on_save_ui_to_config: Callable[[], None], + on_ocr_results: Callable[[Sequence[Tuple[str, str, str, str]]], None], + on_refresh_preview: Callable[[], None], +) -> None: + """构建顶部常驻按钮区(保存配置 + 截图+OCR)。""" + top_btns = ttk.Frame(top) + top_btns.pack(side=RIGHT, padx=(20, 0)) + ttk.Button(top_btns, text="保存配置", command=on_save_ui_to_config).pack( + side=LEFT, padx=(0, 8) + ) + # 截图+OCR 按钮放在顶部,方便随时使用 + screenshot_btn = ttk.Button( + top_btns, + text="截图+OCR", + command=lambda: _do_screenshot_impl( + device_id=device_var.get().strip(), + y_offset=int(y_var.get() or 0), + image_var=image_var, + on_line=on_line, + root=root, + on_ocr_results=on_ocr_results, + on_refresh_preview=on_refresh_preview, + ), + ) + screenshot_btn.pack(side=LEFT, padx=(0, 8)) diff --git a/gui/gui_app.py b/gui/gui_app.py index 3c4c485..0249688 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -101,6 +101,7 @@ from gui.components.main_notebook import setup_main_notebook from gui.components.action_buttons import setup_action_buttons from gui.components.config_vars import build_config_vars from gui.components.top_device_bar import setup_top_device_bar +from gui.components.top_buttons import setup_top_buttons from gui.components.periodic_refreshers import ( setup_periodic_refreshers, setup_close_handler, @@ -284,29 +285,6 @@ def main(): ) load_config_to_ui() - # 顶部常驻按钮区域(与设备选择同一行,不随左侧滚动) - top_btns = ttk.Frame(top) - top_btns.pack(side=RIGHT, padx=(20, 0)) - ttk.Button(top_btns, text="保存配置", command=save_ui_to_config).pack( - side=LEFT, padx=(0, 8) - ) - # 截图+OCR 按钮也放在顶部,方便随时使用 - # 具体回调在后文定义,这里先占位,通过 lambda 延迟绑定 - screenshot_btn = ttk.Button( - top_btns, - text="截图+OCR", - command=lambda: _do_screenshot_impl( - device_id=device_var.get().strip(), - y_offset=int(y_var.get() or 0), - image_var=image_var, - on_line=on_line, - root=root, - on_ocr_results=update_ocr_results, - on_refresh_preview=refresh_preview, - ), - ) - screenshot_btn.pack(side=LEFT, padx=(0, 8)) - # 右侧:操作按钮区域(先创建,放在底部) actions = ttk.Frame(right) @@ -483,6 +461,19 @@ def main(): side=LEFT, padx=(10, 0) ) + # 顶部常驻按钮(放在 image_var/update_ocr_results/refresh_preview 都已就绪之后) + setup_top_buttons( + top, + device_var=device_var, + y_var=y_var, + image_var=image_var, + on_line=on_line, + root=root, + on_save_ui_to_config=save_ui_to_config, + on_ocr_results=update_ocr_results, + on_refresh_preview=refresh_preview, + ) + _win_record_handles = setup_win_record_tab( win_record_tab, on_line=on_line,