fix: 修复输出路径问题 — 路径解析改为基于应用目录而非CWD
当从外部目录(如D:\ccc)拖入文件时,输出文件会错误地写入源目录。 根因是所有路径使用相对路径 + os.getcwd() 解析,CWD不同则路径错误。 修复方案: - ConfigManager.get_path() 改为使用 app_root (exe所在目录/脚本所在目录) - 将 22 处裸硬编码 "data/result"/"data/output" 替换为 config.get_path() - 添加 result_folder 到默认配置和 config.ini - 修复 error_utils.py 中的路径匹配字符串 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+16
-10
@@ -11,10 +11,16 @@ from tkinter import messagebox, scrolledtext
|
||||
from .theme import THEMES, get_theme_mode, apply_theme
|
||||
from .ui_widgets import center_window
|
||||
from app.core.utils.file_utils import format_file_size
|
||||
from app.config.settings import ConfigManager
|
||||
|
||||
TOBACCO_PREVIEW_WINDOW = None
|
||||
|
||||
|
||||
def _get_output_dir():
|
||||
"""获取输出目录的绝对路径"""
|
||||
return ConfigManager().get_path('Paths', 'output_folder', fallback='data/output', create=True)
|
||||
|
||||
|
||||
def show_result_preview(command, output):
|
||||
"""显示处理结果预览"""
|
||||
if "ocr" in command:
|
||||
@@ -26,7 +32,7 @@ def show_result_preview(command, output):
|
||||
elif "pipeline" in command:
|
||||
show_pipeline_result_preview(output)
|
||||
else:
|
||||
messagebox.showinfo("处理完成", "操作已成功完成!\n请在data/output目录查看结果。")
|
||||
messagebox.showinfo("处理完成", f"操作已成功完成!\n请在{_get_output_dir()}目录查看结果。")
|
||||
|
||||
|
||||
def show_ocr_result_preview(output):
|
||||
@@ -68,10 +74,10 @@ def show_ocr_result_preview(output):
|
||||
button_frame = tk.Frame(preview)
|
||||
button_frame.pack(pady=10)
|
||||
|
||||
tk.Button(button_frame, text="查看输出文件", command=lambda: os.startfile(os.path.abspath("data/output"))).pack(side=tk.LEFT, padx=10)
|
||||
tk.Button(button_frame, text="查看输出文件", command=lambda: os.startfile(_get_output_dir())).pack(side=tk.LEFT, padx=10)
|
||||
tk.Button(button_frame, text="关闭", command=preview.destroy).pack(side=tk.LEFT, padx=10)
|
||||
else:
|
||||
messagebox.showinfo("OCR处理完成", "OCR处理已完成,请在data/output目录查看结果。")
|
||||
messagebox.showinfo("OCR处理完成", f"OCR处理已完成,请在{_get_output_dir()}目录查看结果。")
|
||||
|
||||
|
||||
def show_excel_result_preview(output):
|
||||
@@ -120,7 +126,7 @@ def show_excel_result_preview(output):
|
||||
tk.Button(button_frame, text="打开所在文件夹", command=lambda: os.startfile(os.path.dirname(output_file))).pack(side=tk.LEFT, padx=5)
|
||||
tk.Button(button_frame, text="关闭", command=preview.destroy).pack(side=tk.LEFT, padx=5)
|
||||
else:
|
||||
messagebox.showinfo("Excel处理完成", "Excel处理已完成,请在data/output目录查看结果。")
|
||||
messagebox.showinfo("Excel处理完成", f"Excel处理已完成,请在{_get_output_dir()}目录查看结果。")
|
||||
|
||||
|
||||
def show_merge_result_preview(output):
|
||||
@@ -159,7 +165,7 @@ def show_merge_result_preview(output):
|
||||
tk.Button(button_frame, text="打开所在文件夹", command=lambda: os.startfile(os.path.dirname(output_file))).pack(side=tk.LEFT, padx=10)
|
||||
tk.Button(button_frame, text="关闭", command=preview.destroy).pack(side=tk.LEFT, padx=10)
|
||||
else:
|
||||
messagebox.showinfo("采购单合并完成", "采购单合并已完成,请在data/output目录查看结果。")
|
||||
messagebox.showinfo("采购单合并完成", f"采购单合并已完成,请在{_get_output_dir()}目录查看结果。")
|
||||
|
||||
|
||||
def show_pipeline_result_preview(output):
|
||||
@@ -250,7 +256,7 @@ def show_pipeline_result_preview(output):
|
||||
tk.Button(button_frame, text="打开Excel文件", command=lambda: os.startfile(output_file)).pack(side=tk.LEFT, padx=10)
|
||||
else:
|
||||
if excel_match or no_files_match or single_file_match:
|
||||
output_dir = os.path.abspath("data/output")
|
||||
output_dir = _get_output_dir()
|
||||
excel_files = [f for f in os.listdir(output_dir) if f.startswith('采购单_') and (f.endswith('.xls') or f.endswith('.xlsx'))]
|
||||
if excel_files:
|
||||
excel_files.sort(key=lambda x: os.path.getmtime(os.path.join(output_dir, x)), reverse=True)
|
||||
@@ -258,7 +264,7 @@ def show_pipeline_result_preview(output):
|
||||
tk.Button(button_frame, text="打开最新Excel文件",
|
||||
command=lambda: os.startfile(latest_file)).pack(side=tk.LEFT, padx=10)
|
||||
|
||||
tk.Button(button_frame, text="查看输出文件夹", command=lambda: os.startfile(os.path.abspath("data/output"))).pack(side=tk.LEFT, padx=10)
|
||||
tk.Button(button_frame, text="查看输出文件夹", command=lambda: os.startfile(_get_output_dir())).pack(side=tk.LEFT, padx=10)
|
||||
tk.Button(button_frame, text="关闭", command=preview.destroy).pack(side=tk.LEFT, padx=10)
|
||||
|
||||
|
||||
@@ -299,7 +305,7 @@ def show_tobacco_result_preview(returncode, output):
|
||||
items_count = int(items_match.group(1).strip())
|
||||
|
||||
if not result_file or not os.path.exists(result_file):
|
||||
default_path = os.path.abspath("data/output/银豹采购单_烟草公司.xls")
|
||||
default_path = os.path.join(_get_output_dir(), "银豹采购单_烟草公司.xls")
|
||||
if os.path.exists(default_path):
|
||||
result_file = default_path
|
||||
|
||||
@@ -353,11 +359,11 @@ def show_tobacco_result_preview(returncode, output):
|
||||
tk.Button(button_frame, text="关闭", command=_close_preview).pack(side=tk.LEFT, padx=5)
|
||||
else:
|
||||
tk.Label(result_frame, text="未找到输出文件", font=("Arial", 12)).pack(anchor=tk.W, padx=20, pady=5)
|
||||
tk.Label(result_frame, text="请检查data/output目录", font=("Arial", 12, "bold"), fg="#dc3545").pack(pady=10)
|
||||
tk.Label(result_frame, text=f"请检查{_get_output_dir()}目录", font=("Arial", 12, "bold"), fg="#dc3545").pack(pady=10)
|
||||
|
||||
button_frame = tk.Frame(preview)
|
||||
button_frame.pack(pady=10)
|
||||
tk.Button(button_frame, text="打开输出目录", command=lambda: os.startfile(os.path.abspath("data/output"))).pack(side=tk.LEFT, padx=5)
|
||||
tk.Button(button_frame, text="打开输出目录", command=lambda: os.startfile(_get_output_dir())).pack(side=tk.LEFT, padx=5)
|
||||
tk.Button(button_frame, text="关闭", command=_close_preview).pack(side=tk.LEFT, padx=5)
|
||||
|
||||
preview.lift()
|
||||
|
||||
Reference in New Issue
Block a user