diff --git a/src/gui.py b/src/gui.py index 69177eb..31189c9 100644 --- a/src/gui.py +++ b/src/gui.py @@ -5,6 +5,7 @@ import threading import tkinter as tk from tkinter import filedialog, messagebox, ttk import pythoncom +import windnd from config import CONFIG_FILE from extractor import detect_and_create_apps, export_single_doc_tables @@ -26,6 +27,12 @@ class BatchTableExtractorApp: self._build_ui() self._on_mode_change() + # 初始化拖拽支持 + try: + windnd.hook_dropfiles(self.root, func=self._on_drop_files) + except Exception as e: + pass + # 窗口关闭时自动持久化配置 self.root.protocol("WM_DELETE_WINDOW", self._on_close) @@ -130,6 +137,37 @@ class BatchTableExtractorApp: if path: self.out_dir_var.set(path) + def _on_drop_files(self, files): + if not files: + return + + try: + # windnd 返回的通常是 gbk 编码的字节串 + dropped_paths = [f.decode("gbk") for f in files] + except Exception: + try: + dropped_paths = [f.decode("utf-8") for f in files] + except Exception: + return + + if not dropped_paths: + return + + first_path = dropped_paths[0] + + if os.path.isdir(first_path): + self.mode_var.set("dir") + self._on_mode_change() + self.src_path_var.set(first_path) + if not self.out_dir_var.get().strip(): + self.out_dir_var.set(first_path) + elif os.path.isfile(first_path): + self.mode_var.set("file") + self._on_mode_change() + self.src_path_var.set(first_path) + if not self.out_dir_var.get().strip(): + self.out_dir_var.set(os.path.dirname(first_path)) + def log(self, msg): self.txt.insert(tk.END, f"{msg}\n") self.txt.see(tk.END)