Files
douyin_guaji-main/gui/gui_app.py
T

390 lines
13 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import logging
import os
import re
import sys
import warnings
# 确保项目根目录在 Python 路径中(支持直接运行 gui/gui_app.py)
_script_dir = os.path.dirname(os.path.abspath(__file__))
_project_root = os.path.dirname(_script_dir)
if _project_root not in sys.path:
sys.path.insert(0, _project_root)
from PIL import ImageTk # type: ignore[import]
# 抽离:ADB/配置/参数结构体到 gui_core.py(便于后续模块化)
from gui.gui_core import (
RunParams,
read_config,
write_config,
)
from douyin.core.path_config import get_logs_dir
from gui.components.mapping_table import create_mapping_tab
from gui.components.wifi_adb_scan_dialog import open_scan_dialog # noqa: F401 re-export
from gui.components.reservations_view import refresh_reservations as _refresh_reservations_impl
from gui.components.high_value_view import refresh_high_value_rooms as _refresh_high_value_rooms_impl
from gui.components.actions_log_view import (
configure_actions_log_colors as _configure_actions_log_colors_impl,
refresh_actions_log as _refresh_actions_log_impl,
)
from gui.components.log_pump import pump_logs as _pump_logs_impl
from gui.components.win_record_view import (
WinRecordView as _WinRecordView,
refresh_win_record as _refresh_win_record_impl,
on_win_list_select as _on_win_list_select_impl,
open_win_folder as _open_win_folder_impl,
load_false_positives as _load_false_positives_impl,
save_false_positives as _save_false_positives_impl,
)
from gui.components.preview_view import (
PreviewView as _PreviewView,
scan_image_files as _scan_image_files_impl,
update_image_list as _update_image_list_impl,
refresh_preview as _refresh_preview_impl,
_load_and_fit_image as _load_and_fit_image_impl,
open_pic_folder as _open_pic_folder_impl,
async_refresh_preview as _async_refresh_preview_impl,
)
from gui.components.wifi_adb_one_click_scan import (
on_scan_one_click as _on_scan_one_click_impl,
_handle_scan_result as _handle_scan_result_impl,
)
from gui.components.logging_config_io import save_logging_config as _save_logging_config_impl
from gui.components.run_params_builder import get_params as _get_params_impl
from gui.components.config_io import (
load_config_to_ui as _load_config_to_ui_impl,
save_ui_to_config as _save_ui_to_config_impl,
)
from gui.components.debug_tab import setup_debug_tab
from gui.components.logging_tab import setup_logging_tab
from gui.components.yanghao_tab import setup_yanghao_tab
from gui.components.reservation_tab import setup_reservation_tab
from gui.components.run_tab import setup_run_tab
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.simple_tabs import (
setup_simple_text_tab,
setup_stats_tab,
setup_text_tab_with_refresh,
setup_actions_log_tab,
setup_ocr_results_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
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.log_tab import setup_log_tab
from gui.components.log_pipeline import setup_log_pipeline
from gui.components.root_window import setup_root_window
from gui.components.periodic_refreshers import (
setup_periodic_refreshers,
setup_close_handler,
)
# 导入日志颜色配置模块
try:
from gui.components.log_color_config import (
get_color_config,
apply_operation_color,
get_operation_type,
)
except ImportError:
# 如果模块不存在,使用空函数作为回退
def get_color_config(theme: str = "litera"):
return {}
def apply_operation_color(*args, **kwargs):
pass
def get_operation_type(line: str) -> str | None:
return None
# 抑制 PyTorch DataLoader 的 pin_memory 警告(当没有 GPU 时,EasyOCR 会触发此警告)
warnings.filterwarnings("ignore", message=".*pin_memory.*", category=UserWarning)
warnings.filterwarnings("ignore", message=".*accelerator.*", category=UserWarning)
# 模块级 logger:内部"日志写入失败 / pump_logs 异常"等错误统一走 logging
_LOGGER = logging.getLogger("gui.gui_app")
def format_gui_log_line(line: str) -> str:
"""将完整日志行转换为 GUI 显示用的精简版本。
实际实现位于 gui.components.log_viewer,此处仅作 re-export,
失败时回退到原始字符串。
"""
try:
from gui.components.log_viewer import format_gui_log_line as _impl
return _impl(line)
except Exception:
return (line or "").rstrip("\n")
def main():
_bundle = setup_root_window(read_config=read_config)
root = _bundle.root
ScrolledText = _bundle.ScrolledText
import tkinter as tk
from tkinter import ttk
try:
from ttkbootstrap.constants import LEFT, RIGHT, BOTH, X, Y
except ImportError:
from tkinter.constants import LEFT, RIGHT, BOTH, X, Y # type: ignore[assignment]
q, _log_writer, on_line, runner = setup_log_pipeline(
format_gui_log_line=format_gui_log_line
)
# 顶部:设备选择 + Y偏移(其余运行参数放到左侧分组,避免重复)
top = ttk.Frame(root)
top.pack(fill=X, padx=12, pady=10)
_top_bar = setup_top_device_bar(top, root=root, on_line=on_line)
device_var = _top_bar.device_var
refresh_devices = _top_bar.refresh_devices
def save_logging_config():
_save_logging_config_impl(
vars_=_vars_,
read_config=read_config,
write_config=write_config,
on_line=on_line,
)
# 配置区(提前读取配置,供后续使用)
cfg = read_config()
_vars_ = build_config_vars(cfg)
gui_theme_var = _vars_["gui_theme_var"]
y_var = _vars_["y_var"]
theme_getter = lambda: str(gui_theme_var.get() or "litera")
ttk.Label(top, text="Y偏移:").pack(side=LEFT)
ttk.Spinbox(top, from_=-200, to=200, textvariable=y_var, width=6).pack(
side=LEFT, padx=(0, 12)
)
# 中部:左侧配置(使用Notebook标签页)+ 右侧日志/预览(支持拖拽调整宽度)
mid = ttk.PanedWindow(root, orient="horizontal")
mid.pack(fill=BOTH, expand=True, padx=12, pady=(0, 12))
left_wrap = ttk.Frame(mid)
right = ttk.Frame(mid)
mid.add(left_wrap, weight=0) # 左侧配置区域
mid.add(right, weight=1) # 右侧日志/预览区域
# 左侧使用Notebook标签页组织配置
left_notebook = ttk.Notebook(left_wrap)
left_notebook.pack(fill=BOTH, expand=True)
# 创建左侧配置标签页
setup_run_tab(left_notebook, vars_=_vars_)
setup_filter_tab(left_notebook, vars_=_vars_)
setup_reservation_tab(left_notebook, vars_=_vars_)
_, yg_comment_texts, yg_like_keywords_text, yg_fav_keywords_text = setup_yanghao_tab(left_notebook, vars_=_vars_)
setup_debug_tab(left_notebook, vars_=_vars_)
setup_logging_tab(left_notebook, vars_=_vars_, on_save_logging=save_logging_config)
want_text, not_want_text = setup_keywords_tab(left_notebook, ScrolledText_cls=ScrolledText)
_config_io_texts = {
"not_want_text": not_want_text,
"want_text": want_text,
"yg_comment_texts": yg_comment_texts,
"yg_fav_keywords_text": yg_fav_keywords_text,
"yg_like_keywords_text": yg_like_keywords_text,
}
def load_config_to_ui():
nonlocal cfg
cfg = _load_config_to_ui_impl(
vars_=_vars_,
texts_=_config_io_texts,
read_config=read_config,
)
def save_ui_to_config():
_save_ui_to_config_impl(
vars_=_vars_,
texts_=_config_io_texts,
cfg=cfg,
read_config=read_config,
write_config=write_config,
on_line=on_line,
)
load_config_to_ui()
# 右侧:操作按钮区域(先创建,放在底部)
actions = ttk.Frame(right)
def _open_realtime_viewer():
"""委托到 gui/components/realtime_viewer.open_realtime_viewer。"""
from gui.components.realtime_viewer import open_realtime_viewer
open_realtime_viewer(root)
# 右侧:Notebook(日志 / 截图预览 / 统计信息 / 预约队列 / 高价值房间)
_main_nb = setup_main_notebook(
right,
on_open_realtime_viewer=_open_realtime_viewer,
)
notebook = _main_nb.notebook
log_tab = _main_nb.log_tab
preview_tab = _main_nb.preview_tab
stats_tab = _main_nb.stats_tab
reservation_tab = _main_nb.reservation_tab
high_value_tab = _main_nb.high_value_tab
actions_log_tab = _main_nb.actions_log_tab
realtime_tab = _main_nb.realtime_tab
ocr_results_tab = _main_nb.ocr_results_tab
win_record_tab = _main_nb.win_record_tab
# 使用模块化组件创建映射表并加入 Notebook
mapping_tab = create_mapping_tab(root, notebook, cfg)
notebook.add(mapping_tab, text="映射表")
# 日志区
_log_tab_handles = setup_log_tab(
log_tab,
runner=runner,
ScrolledText_cls=ScrolledText,
theme_getter=theme_getter,
)
log_text = _log_tab_handles.log_text
log_filter_var = _log_tab_handles.log_filter_var
refresh_run_log = _log_tab_handles.refresh_run_log
setup_realtime_tab(
realtime_tab,
vars_={
"show_actions_var": _main_nb.show_actions_var,
"show_screenshot_var": _main_nb.show_screenshot_var,
"auto_scroll_var": _main_nb.auto_scroll_var,
},
root=root,
ScrolledText_cls=ScrolledText,
logs_dir_getter=get_logs_dir,
read_config=read_config,
)
_preview_handles = setup_preview_tab(
preview_tab,
on_line=on_line,
root=root,
)
_preview_view = _preview_handles.preview_view
image_var = _preview_handles.image_var
refresh_preview = _preview_handles.refresh_preview
status_var = tk.StringVar(value="就绪")
def get_params() -> RunParams:
return _get_params_impl({**_vars_, "device_var": device_var})
# 先打包 actions 到底部(不 expand,固定在底部)
actions.pack(fill=X, pady=(10, 0), padx=12, side="bottom")
# 然后打包 notebook 填充剩余空间(在 actions 之上)
notebook.pack(fill=BOTH, expand=True)
setup_action_buttons(
actions,
runner=runner,
status_var=status_var,
device_var=device_var,
on_line=on_line,
save_ui_to_config=save_ui_to_config,
get_params=get_params,
)
def pump_logs():
_pump_logs_impl(
q,
text_widget=log_text,
filter_getter=lambda: log_filter_var.get().strip(),
theme_getter=theme_getter,
root=root,
is_running_getter=runner.is_running,
status_getter=status_var.get,
status_setter=status_var.set,
)
# 统计信息显示
refresh_stats = setup_stats_tab(stats_tab, runner=runner, ScrolledText_cls=ScrolledText)
# 预约队列显示
refresh_reservations = setup_text_tab_with_refresh(
reservation_tab,
refresh_impl=_refresh_reservations_impl,
ScrolledText_cls=ScrolledText,
)
# 高价值房间显示
refresh_high_value_rooms = setup_text_tab_with_refresh(
high_value_tab,
refresh_impl=_refresh_high_value_rooms_impl,
ScrolledText_cls=ScrolledText,
)
# 行为日志显示(点击/识别/耗时)
refresh_actions_log = setup_actions_log_tab(
actions_log_tab,
ScrolledText_cls=ScrolledText,
theme_getter=theme_getter,
)
# OCR结果显示
update_ocr_results, _ = setup_ocr_results_tab(
ocr_results_tab, ScrolledText_cls=ScrolledText,
)
# 顶部常驻按钮(放在 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,
ScrolledText_cls=ScrolledText,
open_realtime_viewer=_open_realtime_viewer,
)
refresh_win_record = _win_record_handles.refresh_win_record
pump_logs()
setup_periodic_refreshers(
root,
image_var=image_var,
async_refresh_preview_impl=_async_refresh_preview_impl,
preview_view=_preview_view,
refresh_stats=refresh_stats,
refresh_reservations=refresh_reservations,
refresh_high_value_rooms=refresh_high_value_rooms,
refresh_actions_log=refresh_actions_log,
refresh_win_record=refresh_win_record,
runner=runner,
)
setup_close_handler(root, runner=runner)
refresh_devices()
root.mainloop()
if __name__ == "__main__":
try:
from douyin.core.version import print_version_banner
print_version_banner("gui_app")
except Exception:
pass
main()