refactor(gui): 抽顶部常驻按钮区到 components/top_buttons (527 → 518)

This commit is contained in:
Your Name committed 2026-05-30 17:55:20 +08:00
1 parent b8f2806ffc
commit a1562a513d
2 files changed
+62 -23

No files matched your search

+48
View File
@@ -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))
+14 -23
View File
@@ -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,