chore: Clean up and refactor codebase for improved maintainability
- Removed obsolete command-line test parameters from `run_cli.py` to streamline functionality. - Introduced a new `ocr_preprocess.py` module for unified OCR image preprocessing, enhancing recognition stability. - Updated various components to utilize the new preprocessing function, improving code organization and readability. - Adjusted configuration values in `config.json` for better performance and consistency. - Added comprehensive technical documentation for the Douyin project, detailing code modification standards and operational guidelines. - Cleaned up excessive blank lines across multiple files to enhance overall readability.
This commit is contained in:
1 parent
efe6e2252f
commit
223c127e11
70 files changed
+17121
-1752
No files matched your search
+370
-144
@@ -27,6 +27,22 @@ from gui.gui_core import (
|
||||
)
|
||||
from path_config import get_logs_dir
|
||||
|
||||
# 导入日志颜色配置模块
|
||||
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)
|
||||
@@ -1087,7 +1103,7 @@ def main():
|
||||
|
||||
row4c = ttk.Frame(fudai_find_box)
|
||||
row4c.pack(fill=X, padx=10, pady=(0, 10))
|
||||
ttk.Label(row4c, text="直播间检测阈值:").pack(side=LEFT)
|
||||
ttk.Label(row4c, text="直播间检测阈值(白色像素比例):").pack(side=LEFT)
|
||||
ttk.Spinbox(
|
||||
row4c,
|
||||
from_=0.0,
|
||||
@@ -1096,7 +1112,7 @@ def main():
|
||||
textvariable=live_room_match_threshold_var,
|
||||
width=6,
|
||||
).pack(side=LEFT)
|
||||
ttk.Label(row4c, text="(0.0~1.0,默认0.7,值越高越严格)").pack(
|
||||
ttk.Label(row4c, text="(0.0~1.0,默认0.8,多边形区域白色像素比例>=此值判定为在直播间)").pack(
|
||||
side=LEFT, padx=(6, 0)
|
||||
)
|
||||
|
||||
@@ -1623,7 +1639,7 @@ def main():
|
||||
cfg2["stage_diag_max_step_time"] = float(
|
||||
stage_diag_max_step_time_var.get() or 2.0
|
||||
)
|
||||
cfg2["enable_ocr_mode1"] = bool(enable_ocr_mode1_var.get())
|
||||
cfg2["enable_ocr_mode1"] = bool(cfg.get("enable_ocr_mode1", False))
|
||||
cfg2["fudai_template_match_enabled"] = bool(
|
||||
fudai_template_match_enabled_var.get()
|
||||
)
|
||||
@@ -1941,6 +1957,7 @@ def main():
|
||||
high_value_tab = ttk.Frame(notebook)
|
||||
actions_log_tab = ttk.Frame(notebook)
|
||||
ocr_results_tab = ttk.Frame(notebook)
|
||||
win_record_tab = ttk.Frame(notebook)
|
||||
notebook.add(log_tab, text="运行日志")
|
||||
notebook.add(preview_tab, text="截图预览")
|
||||
notebook.add(stats_tab, text="统计信息")
|
||||
@@ -1948,6 +1965,7 @@ def main():
|
||||
notebook.add(high_value_tab, text="高价值房间")
|
||||
notebook.add(actions_log_tab, text="行为日志")
|
||||
notebook.add(ocr_results_tab, text="OCR结果")
|
||||
notebook.add(win_record_tab, text="中奖记录")
|
||||
|
||||
# 日志区
|
||||
log_box = ttk.Frame(log_tab)
|
||||
@@ -2100,6 +2118,19 @@ def main():
|
||||
log_text.tag_configure("log_critical", foreground="#F44336", font=("Consolas", 10, "bold")) # 加粗亮红色
|
||||
log_text.tag_configure("log_performance", foreground="#81C784") # 亮绿色,性能数据
|
||||
log_text.tag_configure("log_module", foreground="#BA68C8") # 亮紫色,模块标识
|
||||
|
||||
# 配置操作类型颜色标签(使用模块化配置)
|
||||
try:
|
||||
color_config = get_color_config(current_theme)
|
||||
for op_type, colors in color_config.items():
|
||||
tag_name = f"op_{op_type}"
|
||||
log_text.tag_configure(
|
||||
tag_name,
|
||||
foreground=colors["foreground"],
|
||||
background=colors["background"],
|
||||
)
|
||||
except Exception:
|
||||
pass # 忽略操作类型颜色配置失败
|
||||
except Exception:
|
||||
pass # 忽略标签配置失败
|
||||
|
||||
@@ -2273,7 +2304,6 @@ def main():
|
||||
0 if infinite_loop_var.get() else int(wait_minutes_var.get() or 5)
|
||||
)
|
||||
return RunParams(
|
||||
enable_ocr_mode1=bool(enable_ocr_mode1_var.get()),
|
||||
device_id=device_var.get().strip(),
|
||||
y_pianyi=int(y_var.get()),
|
||||
needswitch=bool(needswitch_var.get()),
|
||||
@@ -2287,26 +2317,15 @@ def main():
|
||||
auto_join_fansclub=bool(auto_join_var.get()),
|
||||
fansclub_fail_action=str(fansclub_action_var.get() or "stay").lower(),
|
||||
# Web端新增参数
|
||||
high_value_room_revisit_enabled=bool(
|
||||
high_value_room_revisit_enabled_var.get()
|
||||
),
|
||||
high_value_room_revisit_only_current_run=bool(
|
||||
high_value_room_revisit_only_current_run_var.get()
|
||||
),
|
||||
reservation_back_verify_enabled=bool(
|
||||
reservation_back_verify_enabled_var.get()
|
||||
),
|
||||
reservation_back_swipe_delay=float(
|
||||
reservation_back_swipe_delay_var.get() or 0
|
||||
),
|
||||
reservation_preempt_min_value=float(
|
||||
reservation_preempt_min_value_var.get() or 0
|
||||
),
|
||||
fudai_find_max_attempts=int(fudai_find_max_attempts_var.get() or 0),
|
||||
high_value_room_revisit_enabled=bool(high_value_room_revisit_enabled_var.get()),
|
||||
high_value_room_revisit_only_current_run=bool(high_value_room_revisit_only_current_run_var.get()),
|
||||
reservation_back_verify_enabled=bool(reservation_back_verify_enabled_var.get()),
|
||||
reservation_back_swipe_delay=float(reservation_back_swipe_delay_var.get() or 1.0),
|
||||
reservation_preempt_min_value=float(reservation_preempt_min_value_var.get() or 1000),
|
||||
fudai_find_max_attempts=int(fudai_find_max_attempts_var.get() or 5),
|
||||
enable_ocr_mode1=bool(enable_ocr_mode1_var.get()),
|
||||
fudai_template_match_enabled=bool(fudai_template_match_enabled_var.get()),
|
||||
fudai_template_match_threshold=float(
|
||||
fudai_template_match_threshold_var.get() or 0.7
|
||||
),
|
||||
fudai_template_match_threshold=float(fudai_template_match_threshold_var.get() or 0.7),
|
||||
)
|
||||
|
||||
def start_run():
|
||||
@@ -2354,57 +2373,7 @@ def main():
|
||||
side=LEFT, padx=(0, 8)
|
||||
)
|
||||
|
||||
ttk.Separator(actions, orient="vertical").pack(side=LEFT, fill=Y, padx=10, pady=3)
|
||||
test_seconds_var = tk.IntVar(
|
||||
value=int(cfg.get("yanghao_recommend_duration_seconds", 60) or 60)
|
||||
)
|
||||
ttk.Label(actions, text="测试养号(秒):").pack(side=LEFT)
|
||||
ttk.Spinbox(
|
||||
actions, from_=10, to=3600, increment=10, textvariable=test_seconds_var, width=8
|
||||
).pack(side=LEFT, padx=(0, 8))
|
||||
|
||||
def start_test_yanghao():
|
||||
if runner.is_running():
|
||||
on_line("[运行] 已在运行中")
|
||||
return
|
||||
save_ui_to_config()
|
||||
secs = int(test_seconds_var.get() or 90)
|
||||
dev = device_var.get().strip()
|
||||
on_line(f"[测试] 启动养号流程:device={dev or '(auto)'} seconds={secs}")
|
||||
status_var.set("运行中")
|
||||
try:
|
||||
runner.start_test_yanghao_flow(dev, secs)
|
||||
except Exception as e:
|
||||
status_var.set("启动失败")
|
||||
on_line(f"[错误] {e}")
|
||||
|
||||
ttk.Button(actions, text="测试养号流程", command=start_test_yanghao).pack(side=LEFT)
|
||||
|
||||
ttk.Separator(actions, orient="vertical").pack(side=LEFT, fill=Y, padx=10, pady=3)
|
||||
test_comment_var = tk.StringVar(value="测试评论")
|
||||
ttk.Label(actions, text="测试评论:").pack(side=LEFT)
|
||||
ttk.Entry(actions, textvariable=test_comment_var, width=12).pack(
|
||||
side=LEFT, padx=(0, 8)
|
||||
)
|
||||
|
||||
def start_test_comment():
|
||||
if runner.is_running():
|
||||
on_line("[运行] 已在运行中")
|
||||
return
|
||||
save_ui_to_config()
|
||||
text = test_comment_var.get().strip()
|
||||
dev = device_var.get().strip()
|
||||
on_line(f"[测试] 启动评论测试:device={dev or '(auto)'} text={text}")
|
||||
status_var.set("运行中")
|
||||
try:
|
||||
runner.start_test_comment(dev, text)
|
||||
except Exception as e:
|
||||
status_var.set("启动失败")
|
||||
on_line(f"[错误] {e}")
|
||||
|
||||
ttk.Button(actions, text="测试评论", command=start_test_comment).pack(side=LEFT)
|
||||
|
||||
ttk.Separator(actions, orient="vertical").pack(side=LEFT, fill=Y, padx=10, pady=3)
|
||||
# (已移除:测试养号/测试评论 按钮与相关UI)
|
||||
|
||||
def send_back_key():
|
||||
dev = device_var.get().strip()
|
||||
@@ -2422,37 +2391,56 @@ def main():
|
||||
def pump_logs():
|
||||
changed = False
|
||||
try:
|
||||
# 获取当前主题(用于颜色配置)
|
||||
try:
|
||||
current_theme = str(gui_theme_var.get() or "litera")
|
||||
except Exception:
|
||||
current_theme = "litera"
|
||||
|
||||
while True:
|
||||
line = q.get_nowait()
|
||||
try:
|
||||
# 插入日志行并应用颜色标签
|
||||
start_pos = log_text.index("end-1c")
|
||||
|
||||
# 检查是否需要应用颜色标签
|
||||
tag = None
|
||||
if "[INFO]" in line:
|
||||
tag = "log_info"
|
||||
elif "[WARNING]" in line:
|
||||
tag = "log_warning"
|
||||
elif "[ERROR]" in line:
|
||||
tag = "log_error"
|
||||
elif "[CRITICAL]" in line:
|
||||
tag = "log_critical"
|
||||
elif "elapsed_seconds=" in line or "耗时" in line:
|
||||
tag = "log_performance"
|
||||
elif re.search(r'\[[\w\.]+\]', line):
|
||||
tag = "log_module"
|
||||
|
||||
# 插入文本
|
||||
log_text.insert("end", line + "\n")
|
||||
end_pos = log_text.index("end-1c")
|
||||
|
||||
# 如果有标签,应用到这一行
|
||||
if tag:
|
||||
end_pos = log_text.index("end-1c")
|
||||
# 优先级1: 先检查操作类型颜色(更具体)
|
||||
op_type_applied = False
|
||||
try:
|
||||
op_type = get_operation_type(line)
|
||||
if op_type:
|
||||
apply_operation_color(
|
||||
log_text, start_pos, end_pos, line, current_theme
|
||||
)
|
||||
op_type_applied = True
|
||||
except Exception:
|
||||
pass # 忽略操作类型颜色应用失败
|
||||
|
||||
# 优先级2: 再检查日志级别标签(作为补充,如果操作类型未匹配)
|
||||
# 只有在没有操作类型颜色时才应用级别标签
|
||||
if not op_type_applied:
|
||||
try:
|
||||
log_text.tag_add(tag, start_pos, end_pos)
|
||||
tag = None
|
||||
if "[INFO]" in line:
|
||||
tag = "log_info"
|
||||
elif "[WARNING]" in line:
|
||||
tag = "log_warning"
|
||||
elif "[ERROR]" in line:
|
||||
tag = "log_error"
|
||||
elif "[CRITICAL]" in line:
|
||||
tag = "log_critical"
|
||||
elif "elapsed_seconds=" in line or "耗时" in line:
|
||||
tag = "log_performance"
|
||||
elif re.search(r'\[[\w\.]+\]', line):
|
||||
tag = "log_module"
|
||||
|
||||
if tag:
|
||||
log_text.tag_add(tag, start_pos, end_pos)
|
||||
except Exception:
|
||||
pass # 忽略标签应用失败
|
||||
pass # 忽略级别标签应用失败
|
||||
|
||||
# 不再自动滚动到底部,让用户自由控制滚动位置
|
||||
changed = True
|
||||
@@ -3430,35 +3418,43 @@ def main():
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 配置行为日志颜色标签(与主日志保持一致)
|
||||
try:
|
||||
# 根据主题调整行为日志颜色
|
||||
light_themes = {"litera", "flatly", "journal", "minty", "cosmo"}
|
||||
# 配置行为日志颜色标签(使用统一的颜色配置模块)
|
||||
def configure_actions_log_colors():
|
||||
"""根据当前主题配置行为日志的颜色标签"""
|
||||
try:
|
||||
current_theme = str(gui_theme_var.get() or "litera")
|
||||
# 获取当前主题
|
||||
try:
|
||||
current_theme = str(gui_theme_var.get() or "litera")
|
||||
except Exception:
|
||||
current_theme = "litera"
|
||||
|
||||
# 获取颜色配置
|
||||
color_config = get_color_config(current_theme)
|
||||
|
||||
# 根据主题设置背景和前景色
|
||||
light_themes = {"litera", "flatly", "journal", "minty", "cosmo"}
|
||||
is_light = current_theme.lower() in light_themes
|
||||
if is_light:
|
||||
actions_log_text.configure(background="#FFFFFF", foreground="#2C2C2C", insertbackground="#2C2C2C")
|
||||
else:
|
||||
actions_log_text.configure(background="#1E1E1E", foreground="#E0E0E0", insertbackground="#FFFFFF")
|
||||
|
||||
# 为所有操作类型配置标签
|
||||
for op_type, colors in color_config.items():
|
||||
tag_name = f"op_{op_type}"
|
||||
try:
|
||||
actions_log_text.tag_configure(
|
||||
tag_name,
|
||||
foreground=colors["foreground"],
|
||||
background=colors["background"],
|
||||
)
|
||||
except Exception:
|
||||
pass # 忽略标签配置失败
|
||||
except Exception:
|
||||
current_theme = "litera"
|
||||
|
||||
if current_theme in light_themes:
|
||||
# 明亮主题
|
||||
actions_log_text.configure(background="#FFFFFF", foreground="#2C2C2C", insertbackground="#2C2C2C")
|
||||
actions_log_text.tag_configure("action_timestamp", foreground="#8B8B8B")
|
||||
actions_log_text.tag_configure("action_operation", foreground="#1E88E5") # 蓝色操作
|
||||
actions_log_text.tag_configure("action_ocr", foreground="#388E3C") # 绿色OCR
|
||||
actions_log_text.tag_configure("action_decision", foreground="#F57C00") # 橙色决策
|
||||
actions_log_text.tag_configure("action_state", foreground="#7B1FA2") # 紫色状态
|
||||
actions_log_text.tag_configure("action_performance", foreground="#D32F2F") # 红色性能
|
||||
else:
|
||||
# 暗色主题
|
||||
actions_log_text.configure(background="#1E1E1E", foreground="#E0E0E0", insertbackground="#FFFFFF")
|
||||
actions_log_text.tag_configure("action_timestamp", foreground="#A0A0A0")
|
||||
actions_log_text.tag_configure("action_operation", foreground="#64B5F6") # 亮蓝色操作
|
||||
actions_log_text.tag_configure("action_ocr", foreground="#81C784") # 亮绿色OCR
|
||||
actions_log_text.tag_configure("action_decision", foreground="#FFB74D") # 金色决策
|
||||
actions_log_text.tag_configure("action_state", foreground="#BA68C8") # 亮紫色状态
|
||||
actions_log_text.tag_configure("action_performance", foreground="#EF5350") # 亮红色性能
|
||||
except Exception:
|
||||
pass # 忽略颜色配置失败
|
||||
pass # 忽略颜色配置失败
|
||||
|
||||
# 初始配置
|
||||
configure_actions_log_colors()
|
||||
|
||||
actions_filter_var = tk.StringVar(value="")
|
||||
|
||||
@@ -3515,6 +3511,12 @@ def main():
|
||||
|
||||
actions_log_text.delete("1.0", "end")
|
||||
if tail:
|
||||
# 获取当前主题(用于颜色配置)
|
||||
try:
|
||||
current_theme = str(gui_theme_var.get() or "litera")
|
||||
except Exception:
|
||||
current_theme = "litera"
|
||||
|
||||
# 逐行插入并应用颜色标签
|
||||
for i, line in enumerate(tail):
|
||||
start_pos = actions_log_text.index("end-1c")
|
||||
@@ -3524,29 +3526,18 @@ def main():
|
||||
actions_log_text.insert("end", "\n")
|
||||
actions_log_text.insert("end", line)
|
||||
|
||||
# 根据内容类型应用颜色标签
|
||||
# 使用统一的颜色配置模块应用颜色
|
||||
end_pos = actions_log_text.index("end")
|
||||
tag = None
|
||||
|
||||
if "操作:" in line or "执行" in line:
|
||||
tag = "action_operation"
|
||||
elif "OCR" in line or "识别" in line:
|
||||
tag = "action_ocr"
|
||||
elif "决策" in line or "判断" in line:
|
||||
tag = "action_decision"
|
||||
elif "状态" in line or "阶段" in line:
|
||||
tag = "action_state"
|
||||
elif "耗时" in line or "时间" in line or "性能" in line:
|
||||
tag = "action_performance"
|
||||
elif re.search(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}', line):
|
||||
tag = "action_timestamp"
|
||||
|
||||
# 应用标签
|
||||
if tag:
|
||||
try:
|
||||
actions_log_text.tag_add(tag, start_pos, end_pos)
|
||||
except Exception:
|
||||
pass # 忽略标签应用失败
|
||||
try:
|
||||
apply_operation_color(
|
||||
actions_log_text,
|
||||
start_pos,
|
||||
end_pos,
|
||||
line,
|
||||
current_theme
|
||||
)
|
||||
except Exception:
|
||||
pass # 忽略颜色应用失败
|
||||
else:
|
||||
actions_log_text.insert("1.0", "暂无匹配的行为日志")
|
||||
|
||||
@@ -3719,6 +3710,240 @@ def main():
|
||||
side=LEFT, padx=(10, 0)
|
||||
)
|
||||
|
||||
# 中奖记录显示
|
||||
win_record_top = ttk.Frame(win_record_tab)
|
||||
win_record_top.pack(fill=X, padx=10, pady=(10, 0))
|
||||
|
||||
win_record_left = ttk.Frame(win_record_tab)
|
||||
win_record_left.pack(side=LEFT, fill=BOTH, expand=True, padx=(10, 5), pady=10)
|
||||
|
||||
win_record_right = ttk.Frame(win_record_tab)
|
||||
win_record_right.pack(side=RIGHT, fill=BOTH, expand=False, padx=(5, 10), pady=10, ipadx=10)
|
||||
|
||||
# 左侧:中奖截图列表
|
||||
win_list_label = ttk.Label(win_record_left, text="中奖截图列表:")
|
||||
win_list_label.pack(anchor="w", padx=(0, 10), pady=(0, 5))
|
||||
|
||||
win_list_frame = ttk.Frame(win_record_left)
|
||||
win_list_frame.pack(fill=BOTH, expand=True)
|
||||
|
||||
win_list_scrollbar = ttk.Scrollbar(win_list_frame)
|
||||
win_list_scrollbar.pack(side=RIGHT, fill=Y)
|
||||
|
||||
win_listbox = tk.Listbox(win_list_frame, yscrollcommand=win_list_scrollbar.set, font=("Consolas", 9))
|
||||
win_listbox.pack(side=LEFT, fill=BOTH, expand=True)
|
||||
win_list_scrollbar.config(command=win_listbox.yview)
|
||||
|
||||
# 存储文件路径映射(索引 -> 文件路径)
|
||||
_win_file_paths: dict[int, str] = {}
|
||||
|
||||
# 右侧:截图预览
|
||||
win_preview_label = ttk.Label(win_record_right, text="截图预览:")
|
||||
win_preview_label.pack(anchor="w", pady=(0, 5))
|
||||
|
||||
win_preview_frame = ttk.Frame(win_record_right)
|
||||
win_preview_frame.pack(fill=BOTH, expand=True)
|
||||
|
||||
win_image_label = ttk.Label(win_preview_frame)
|
||||
win_image_label.pack(fill=BOTH, expand=True)
|
||||
_win_img_ref: dict[str, Any] = {"photo": None}
|
||||
|
||||
def scan_win_images():
|
||||
"""扫描中奖截图目录"""
|
||||
try:
|
||||
from path_config import get_pic_dir
|
||||
pic_dir = get_pic_dir()
|
||||
except Exception:
|
||||
import path_config
|
||||
pic_dir = path_config.get_pic_dir()
|
||||
|
||||
prize_dir = os.path.join(pic_dir, "lottery_prize_captures")
|
||||
win_images = []
|
||||
|
||||
if os.path.exists(prize_dir):
|
||||
for file in os.listdir(prize_dir):
|
||||
if file.lower().endswith((".png", ".jpg", ".jpeg")):
|
||||
file_path = os.path.join(prize_dir, file)
|
||||
# 获取文件修改时间
|
||||
try:
|
||||
mtime = os.path.getmtime(file_path)
|
||||
win_images.append((file, file_path, mtime))
|
||||
except Exception:
|
||||
win_images.append((file, file_path, 0))
|
||||
|
||||
# 按时间倒序排序(最新的在前)
|
||||
win_images.sort(key=lambda x: x[2], reverse=True)
|
||||
return win_images
|
||||
|
||||
def refresh_win_record():
|
||||
"""刷新中奖记录列表"""
|
||||
try:
|
||||
win_images = scan_win_images()
|
||||
win_listbox.delete(0, tk.END)
|
||||
_win_file_paths.clear()
|
||||
|
||||
for idx, (file, file_path, mtime) in enumerate(win_images):
|
||||
# 格式化显示:文件名 + 时间
|
||||
try:
|
||||
from datetime import datetime
|
||||
dt = datetime.fromtimestamp(mtime)
|
||||
time_str = dt.strftime("%Y-%m-%d %H:%M:%S")
|
||||
display_text = f"{file} ({time_str})"
|
||||
except Exception:
|
||||
display_text = file
|
||||
|
||||
win_listbox.insert(tk.END, display_text)
|
||||
# 存储文件路径映射
|
||||
_win_file_paths[idx] = file_path
|
||||
|
||||
if win_images:
|
||||
on_line(f"[中奖记录] 扫描到 {len(win_images)} 张中奖截图")
|
||||
else:
|
||||
on_line("[中奖记录] 暂无中奖截图")
|
||||
except Exception as e:
|
||||
on_line(f"[中奖记录] 刷新失败:{e}")
|
||||
|
||||
def on_win_list_select(event):
|
||||
"""当选择中奖截图时,显示预览"""
|
||||
try:
|
||||
selection = win_listbox.curselection()
|
||||
if not selection:
|
||||
return
|
||||
|
||||
index = selection[0]
|
||||
# 从映射中获取文件路径
|
||||
if index in _win_file_paths:
|
||||
file_path = _win_file_paths[index]
|
||||
_load_win_image(file_path)
|
||||
except Exception as e:
|
||||
on_line(f"[中奖记录] 预览失败:{e}")
|
||||
|
||||
def _load_win_image(img_path: str):
|
||||
"""加载并显示中奖截图"""
|
||||
if not os.path.exists(img_path):
|
||||
win_image_label.configure(image="")
|
||||
_win_img_ref["photo"] = None
|
||||
return
|
||||
|
||||
try:
|
||||
img = Image.open(img_path)
|
||||
img.load()
|
||||
img = img.convert("RGB")
|
||||
except Exception as e:
|
||||
on_line(f"[中奖记录] 图片加载失败:{e}")
|
||||
return
|
||||
|
||||
# 获取预览区域尺寸
|
||||
try:
|
||||
win_preview_frame.update_idletasks()
|
||||
w = win_preview_frame.winfo_width()
|
||||
h = win_preview_frame.winfo_height()
|
||||
if w <= 1 or h <= 1:
|
||||
w, h = 400, 600
|
||||
except Exception:
|
||||
w, h = 400, 600
|
||||
|
||||
max_w = max(200, w - 20)
|
||||
max_h = max(300, h - 20)
|
||||
|
||||
try:
|
||||
resample = Image.Resampling.LANCZOS
|
||||
except AttributeError:
|
||||
resample = 1
|
||||
|
||||
img.thumbnail((max_w, max_h), resample)
|
||||
photo = ImageTk.PhotoImage(img)
|
||||
_win_img_ref["photo"] = photo
|
||||
win_image_label.configure(image=photo)
|
||||
|
||||
win_listbox.bind("<<ListboxSelect>>", on_win_list_select)
|
||||
|
||||
ttk.Button(win_record_top, text="刷新列表", command=refresh_win_record).pack(side=LEFT, padx=(0, 10))
|
||||
|
||||
def open_win_folder():
|
||||
"""打开中奖截图目录"""
|
||||
try:
|
||||
from path_config import get_pic_dir
|
||||
pic_dir = get_pic_dir()
|
||||
except Exception:
|
||||
import path_config
|
||||
pic_dir = path_config.get_pic_dir()
|
||||
|
||||
prize_dir = os.path.join(pic_dir, "lottery_prize_captures")
|
||||
os.makedirs(prize_dir, exist_ok=True)
|
||||
try:
|
||||
os.startfile(prize_dir) # type: ignore[attr-defined]
|
||||
except Exception as e:
|
||||
on_line(f"[中奖记录] 打开目录失败:{e}")
|
||||
|
||||
ttk.Button(win_record_top, text="打开目录", command=open_win_folder).pack(side=LEFT)
|
||||
|
||||
# 误识别记录管理
|
||||
false_positive_frame = ttk.LabelFrame(win_record_right, text="误识别记录管理")
|
||||
false_positive_frame.pack(fill=X, pady=(10, 0))
|
||||
|
||||
false_positive_text = ScrolledText(false_positive_frame, height=6)
|
||||
false_positive_text.pack(fill=X, padx=10, pady=10)
|
||||
|
||||
def load_false_positives():
|
||||
"""加载误识别记录"""
|
||||
try:
|
||||
from path_config import get_pic_dir
|
||||
pic_dir = get_pic_dir()
|
||||
except Exception:
|
||||
import path_config
|
||||
pic_dir = path_config.get_pic_dir()
|
||||
|
||||
false_positive_file = os.path.join(pic_dir, "lottery_prize_captures", "false_positives.json")
|
||||
if not os.path.exists(false_positive_file):
|
||||
false_positive_text.delete("1.0", "end")
|
||||
false_positive_text.insert("1.0", "暂无误识别记录")
|
||||
return
|
||||
|
||||
try:
|
||||
import json
|
||||
with open(false_positive_file, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
false_positives = data.get("false_positives", [])
|
||||
false_positive_text.delete("1.0", "end")
|
||||
if false_positives:
|
||||
false_positive_text.insert("1.0", "\n".join(false_positives))
|
||||
else:
|
||||
false_positive_text.insert("1.0", "暂无误识别记录")
|
||||
except Exception as e:
|
||||
false_positive_text.delete("1.0", "end")
|
||||
false_positive_text.insert("1.0", f"加载失败:{e}")
|
||||
|
||||
def save_false_positives():
|
||||
"""保存误识别记录"""
|
||||
try:
|
||||
from path_config import get_pic_dir
|
||||
pic_dir = get_pic_dir()
|
||||
except Exception:
|
||||
import path_config
|
||||
pic_dir = path_config.get_pic_dir()
|
||||
|
||||
false_positive_file = os.path.join(pic_dir, "lottery_prize_captures", "false_positives.json")
|
||||
os.makedirs(os.path.dirname(false_positive_file), exist_ok=True)
|
||||
|
||||
content = false_positive_text.get("1.0", "end-1c")
|
||||
lines = [line.strip() for line in content.splitlines() if line.strip()]
|
||||
|
||||
try:
|
||||
import json
|
||||
with open(false_positive_file, "w", encoding="utf-8") as f:
|
||||
json.dump({"false_positives": lines}, f, ensure_ascii=False, indent=2)
|
||||
on_line(f"[中奖记录] 已保存 {len(lines)} 条误识别记录")
|
||||
except Exception as e:
|
||||
on_line(f"[中奖记录] 保存失败:{e}")
|
||||
|
||||
false_positive_btn_frame = ttk.Frame(false_positive_frame)
|
||||
false_positive_btn_frame.pack(fill=X, padx=10, pady=(0, 10))
|
||||
ttk.Button(false_positive_btn_frame, text="刷新", command=load_false_positives).pack(side=LEFT, padx=(0, 5))
|
||||
ttk.Button(false_positive_btn_frame, text="保存", command=save_false_positives).pack(side=LEFT)
|
||||
|
||||
load_false_positives()
|
||||
|
||||
pump_logs()
|
||||
|
||||
def delayed_refresh_preview() -> None:
|
||||
@@ -3730,6 +3955,7 @@ def main():
|
||||
root.after(500, refresh_reservations) # type: ignore[arg-type]
|
||||
root.after(500, refresh_high_value_rooms) # type: ignore[arg-type]
|
||||
root.after(500, refresh_actions_log) # type: ignore[arg-type]
|
||||
root.after(500, refresh_win_record) # type: ignore[arg-type]
|
||||
|
||||
# 定期刷新行为日志(每2秒)
|
||||
def auto_refresh_actions_log() -> None:
|
||||
@@ -3771,4 +3997,4 @@ if __name__ == "__main__":
|
||||
print_version_banner("gui_app")
|
||||
except Exception:
|
||||
pass
|
||||
main()
|
||||
main()
|
||||
Reference in new issue
Block a user