gui拖拽支持

This commit is contained in:
HONOR
2026-09-16 09:53:48 +08:00
parent d41de616d7
commit 147b8932be
+38
View File
@@ -5,6 +5,7 @@ import threading
import tkinter as tk import tkinter as tk
from tkinter import filedialog, messagebox, ttk from tkinter import filedialog, messagebox, ttk
import pythoncom import pythoncom
import windnd
from config import CONFIG_FILE from config import CONFIG_FILE
from extractor import detect_and_create_apps, export_single_doc_tables from extractor import detect_and_create_apps, export_single_doc_tables
@@ -26,6 +27,12 @@ class BatchTableExtractorApp:
self._build_ui() self._build_ui()
self._on_mode_change() 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) self.root.protocol("WM_DELETE_WINDOW", self._on_close)
@@ -130,6 +137,37 @@ class BatchTableExtractorApp:
if path: if path:
self.out_dir_var.set(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): def log(self, msg):
self.txt.insert(tk.END, f"{msg}\n") self.txt.insert(tk.END, f"{msg}\n")
self.txt.see(tk.END) self.txt.see(tk.END)