Files
douyin_guaji-main/gui/gui_app.py
T

179 lines
5.7 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 os
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)
# 抽离: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.wifi_adb_scan_dialog import open_scan_dialog # noqa: F401 re-export
from gui.components.preview_view import (
async_refresh_preview as _async_refresh_preview_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, # noqa: F401 re-export
save_ui_to_config as _save_ui_to_config_impl, # noqa: F401 re-export
)
from gui.components.simple_tabs import (
setup_simple_text_tab, # noqa: F401 re-export
)
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_pipeline import setup_log_pipeline
from gui.components.root_window import setup_root_window
from gui.components.left_config_area import setup_left_config_area
from gui.components.right_main_area import setup_right_main_area
from gui.components.periodic_refreshers import (
setup_periodic_refreshers,
setup_close_handler,
)
# 抑制 PyTorch DataLoader 的 pin_memory 警告(当没有 GPU 时,EasyOCR 会触发此警告)
warnings.filterwarnings("ignore", message=".*pin_memory.*", category=UserWarning)
warnings.filterwarnings("ignore", message=".*accelerator.*", category=UserWarning)
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, BOTH, X
except ImportError:
from tkinter.constants import LEFT, BOTH, X # 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)
)
_left_area = setup_left_config_area(
root,
vars_=_vars_,
cfg=cfg,
ScrolledText_cls=ScrolledText,
on_save_logging=save_logging_config,
read_config=read_config,
write_config=write_config,
on_line=on_line,
)
right = _left_area.right_frame
save_ui_to_config = _left_area.save_ui_to_config
def get_params() -> RunParams:
return _get_params_impl({**_vars_, "device_var": device_var})
_right = setup_right_main_area(
right,
root=root,
cfg=cfg,
runner=runner,
on_line=on_line,
log_queue=q,
device_var=device_var,
save_ui_to_config=save_ui_to_config,
get_params_from_vars=get_params,
ScrolledText_cls=ScrolledText,
theme_getter=theme_getter,
logs_dir_getter=get_logs_dir,
read_config=read_config,
)
# 顶部常驻按钮(image_var/update_ocr_results/refresh_preview 都已就绪)
setup_top_buttons(
top,
device_var=device_var,
y_var=y_var,
image_var=_right.image_var,
on_line=on_line,
root=root,
on_save_ui_to_config=save_ui_to_config,
on_ocr_results=_right.update_ocr_results,
on_refresh_preview=_right.refresh_preview,
)
setup_periodic_refreshers(
root,
image_var=_right.image_var,
async_refresh_preview_impl=_async_refresh_preview_impl,
preview_view=_right.preview_view,
refresh_stats=_right.refresh_stats,
refresh_reservations=_right.refresh_reservations,
refresh_high_value_rooms=_right.refresh_high_value_rooms,
refresh_actions_log=_right.refresh_actions_log,
refresh_win_record=_right.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()