refactor(gui): 抽底部操作按钮区到 components/action_buttons (1417 → 1373)
This commit is contained in:
1 parent
e314161b6c
commit
7a840120b3
2 files changed
+89
-53
No files matched your search
@@ -0,0 +1,80 @@
|
||||
"""底部操作按钮区(开始运行 / 停止 / 强制停止 / 发送返回键 / 状态标签)。
|
||||
|
||||
从 ``gui/gui_app.py`` 抽出。原本是 ``main()`` 内的 widget 创建段 + 4 个按钮回调
|
||||
(``start_run`` / ``stop_run`` / ``force_stop_run`` / ``send_back_key``)。
|
||||
|
||||
依赖通过显式参数注入。状态写回主线程通过 ``status_var`` 实现。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import tkinter as tk
|
||||
from tkinter import ttk
|
||||
from tkinter.constants import LEFT, RIGHT, X
|
||||
from typing import Any, Callable
|
||||
|
||||
|
||||
def setup_action_buttons(
|
||||
actions: ttk.Frame,
|
||||
*,
|
||||
runner: Any,
|
||||
status_var: tk.StringVar,
|
||||
device_var: tk.StringVar,
|
||||
on_line: Callable[[str], None],
|
||||
save_ui_to_config: Callable[[], None],
|
||||
get_params: Callable[[], Any],
|
||||
) -> None:
|
||||
"""构建底部操作按钮区(开始/停止/强制停止/发送返回键 + 状态标签)。"""
|
||||
|
||||
def start_run() -> None:
|
||||
if runner.is_running():
|
||||
on_line("[运行] 已在运行中")
|
||||
return
|
||||
save_ui_to_config()
|
||||
params = get_params()
|
||||
|
||||
wait_display = "∞" if params.wait_minutes == 0 else f"{params.wait_minutes}m"
|
||||
on_line(
|
||||
f"[运行] 启动:device={params.device_id or '(auto)'} y={params.y_pianyi} "
|
||||
f"needswitch={params.needswitch} wait={wait_display} "
|
||||
f"skip_gt={params.skip_if_countdown_gt_seconds}s reset_after_enabled={params.reset_after_swipes_enabled} reset_after={params.reset_after_swipes} "
|
||||
f"value>={params.min_prize_value_yuan} participants=[{params.min_participants},{params.max_participants}] "
|
||||
f"high_value_revisit={params.high_value_room_revisit_enabled} reservation_verify={params.reservation_back_verify_enabled} "
|
||||
f"fudai_find_max={params.fudai_find_max_attempts} yanghao=recommend_flow_only"
|
||||
)
|
||||
status_var.set("运行中")
|
||||
try:
|
||||
runner.start(params)
|
||||
except Exception as e:
|
||||
status_var.set("启动失败")
|
||||
on_line(f"[错误] {e}")
|
||||
|
||||
def stop_run() -> None:
|
||||
runner.stop()
|
||||
status_var.set("已停止")
|
||||
|
||||
def force_stop_run() -> None:
|
||||
"""强制终止后台进程(包括可能残留的子进程)。"""
|
||||
runner.force_stop()
|
||||
status_var.set("已停止(强制)")
|
||||
|
||||
def send_back_key() -> None:
|
||||
dev = device_var.get().strip()
|
||||
try:
|
||||
runner.send_back_key(dev)
|
||||
except Exception as e:
|
||||
on_line(f"[错误] 发送返回键失败: {e}")
|
||||
|
||||
ttk.Button(actions, text="开始运行", command=start_run).pack(side=LEFT, padx=(0, 8))
|
||||
ttk.Button(actions, text="停止", command=stop_run).pack(side=LEFT, padx=(0, 4))
|
||||
ttk.Button(actions, text="强制停止", command=force_stop_run).pack(
|
||||
side=LEFT, padx=(0, 8)
|
||||
)
|
||||
|
||||
# (已移除:测试养号/测试评论 按钮与相关UI)
|
||||
|
||||
ttk.Button(actions, text="发送返回键", command=send_back_key).pack(
|
||||
side=LEFT, padx=(0, 8)
|
||||
)
|
||||
|
||||
ttk.Label(actions, textvariable=status_var).pack(side=RIGHT)
|
||||
+9
-53
@@ -102,6 +102,7 @@ from gui.components.simple_tabs import setup_simple_text_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
|
||||
|
||||
# 导入日志颜色配置模块
|
||||
try:
|
||||
@@ -1212,66 +1213,21 @@ def main():
|
||||
"yg_recommend_duration_var": yg_recommend_duration_var,
|
||||
"enable_back_on_misses_var": enable_back_on_misses_var,
|
||||
})
|
||||
def start_run():
|
||||
if runner.is_running():
|
||||
on_line("[运行] 已在运行中")
|
||||
return
|
||||
save_ui_to_config()
|
||||
params = get_params()
|
||||
|
||||
wait_display = "∞" if params.wait_minutes == 0 else f"{params.wait_minutes}m"
|
||||
on_line(
|
||||
f"[运行] 启动:device={params.device_id or '(auto)'} y={params.y_pianyi} "
|
||||
f"needswitch={params.needswitch} wait={wait_display} "
|
||||
f"skip_gt={params.skip_if_countdown_gt_seconds}s reset_after_enabled={params.reset_after_swipes_enabled} reset_after={params.reset_after_swipes} "
|
||||
f"value>={params.min_prize_value_yuan} participants=[{params.min_participants},{params.max_participants}] "
|
||||
f"high_value_revisit={params.high_value_room_revisit_enabled} reservation_verify={params.reservation_back_verify_enabled} "
|
||||
f"fudai_find_max={params.fudai_find_max_attempts} yanghao=recommend_flow_only"
|
||||
)
|
||||
status_var.set("运行中")
|
||||
try:
|
||||
runner.start(params)
|
||||
except Exception as e:
|
||||
status_var.set("启动失败")
|
||||
on_line(f"[错误] {e}")
|
||||
|
||||
def stop_run():
|
||||
runner.stop()
|
||||
status_var.set("已停止")
|
||||
|
||||
# 先打包 actions 到底部(不 expand,固定在底部)
|
||||
actions.pack(fill=X, pady=(10, 0), padx=12, side="bottom")
|
||||
|
||||
# 然后打包 notebook 填充剩余空间(在 actions 之上)
|
||||
notebook.pack(fill=BOTH, expand=True)
|
||||
|
||||
ttk.Button(actions, text="开始运行", command=start_run).pack(side=LEFT, padx=(0, 8))
|
||||
ttk.Button(actions, text="停止", command=stop_run).pack(side=LEFT, padx=(0, 4))
|
||||
|
||||
def force_stop_run():
|
||||
"""强制终止后台进程(包括可能残留的子进程)。"""
|
||||
runner.force_stop()
|
||||
status_var.set("已停止(强制)")
|
||||
|
||||
ttk.Button(actions, text="强制停止", command=force_stop_run).pack(
|
||||
side=LEFT, padx=(0, 8)
|
||||
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,
|
||||
)
|
||||
|
||||
# (已移除:测试养号/测试评论 按钮与相关UI)
|
||||
|
||||
def send_back_key():
|
||||
dev = device_var.get().strip()
|
||||
try:
|
||||
runner.send_back_key(dev)
|
||||
except Exception as e:
|
||||
on_line(f"[错误] 发送返回键失败: {e}")
|
||||
|
||||
ttk.Button(actions, text="发送返回键", command=send_back_key).pack(
|
||||
side=LEFT, padx=(0, 8)
|
||||
)
|
||||
|
||||
ttk.Label(actions, textvariable=status_var).pack(side=RIGHT)
|
||||
|
||||
def pump_logs():
|
||||
_pump_logs_impl(
|
||||
q,
|
||||
|
||||
Reference in new issue
Block a user