From 35afc5e8dadc62a1eb71c0884395691d6a349a65 Mon Sep 17 00:00:00 2001 From: wood chen Date: Sat, 16 Nov 2024 08:28:11 +0800 Subject: [PATCH] feat(workflows, gui, requirements): Add tkinterdnd2 support for Windows and macOS, update installation and UI for drag and drop --- .github/workflows/release.yml | 7 ++- gui.py | 93 ++++++++++++++++++++++++++++++----- requirements.txt | 9 ++-- 3 files changed, 90 insertions(+), 19 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8b5854e..42420c3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -57,6 +57,8 @@ jobs: run: | python -m pip install --upgrade pip pip install -r requirements.txt + # 确保安装tkinterdnd2 + pip install tkinterdnd2>=0.4.2 pip install pyinstaller # 创建启动器脚本 @@ -107,10 +109,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - # 安装基本依赖,排除tkinterdnd2 - pip install ffmpeg-python Pillow - # 尝试安装tkinterdnd2,但允许失败 - pip install tkinterdnd2 || echo "tkinterdnd2 installation failed, continuing without it" + pip install -r requirements.txt pip install pyinstaller # 创建启动器脚本 diff --git a/gui.py b/gui.py index b03761a..f0c6ee4 100644 --- a/gui.py +++ b/gui.py @@ -9,13 +9,20 @@ from threading import Thread import ffmpeg import traceback # 用于打印详细错误信息 -try: - from tkinterdnd2 import * +# 根据平台选择拖放实现 +PLATFORM = platform.system().lower() - SUPPORT_DND = True -except ImportError: - SUPPORT_DND = False - print("Warning: tkinterdnd2 not available, drag and drop support will be disabled") +if PLATFORM == "windows": + try: + from tkinterdnd2 import DND_FILES, TkinterDnD + + SUPPORT_DND = "tkdnd" + except ImportError: + SUPPORT_DND = None +elif PLATFORM == "darwin": # macOS + SUPPORT_DND = "macos" +else: # Linux or others + SUPPORT_DND = None class FFmpegInstaller: @@ -271,20 +278,59 @@ class VideoToGifConverter: def __init__(self): self.check_ffmpeg_installation() - # 根据是否支持拖放来创建不同的根窗口 - if SUPPORT_DND: + # 创建主窗口 + if SUPPORT_DND == "tkdnd": self.root = TkinterDnD.Tk() else: self.root = tk.Tk() self.root.title("视频转GIF工具") - # 只在支持拖放时启用相关功能 - if SUPPORT_DND: + # 设置拖放支持 + self.setup_dnd() + + # 设置UI + self.setup_ui() + + def setup_dnd(self): + """设置拖放支持""" + if SUPPORT_DND == "tkdnd": + # Windows with tkinterdnd2 self.root.drop_target_register(DND_FILES) self.root.dnd_bind("<>", self.handle_drop) + elif SUPPORT_DND == "macos": + # macOS native drag and drop + self.root.bind("<>", self.handle_macos_drop) + # 启用macOS拖放 + self.root.tk.call("tk_getOpenFile", "-setup") + else: + print("Drag and drop not supported on this platform") - self.setup_ui() + def handle_drop(self, event): + """处理Windows下的文件拖放""" + files = self.root.tk.splitlist(event.data) + self.process_dropped_files(files) + + def handle_macos_drop(self, event): + """处理macOS下的文件拖放""" + # macOS下获取拖放的文件路径 + files = self.root.tk.splitlist(self.root.tk.call("::tk::mac::GetDroppedFiles")) + self.process_dropped_files(files) + + def process_dropped_files(self, files): + """处理拖放的文件""" + # 过滤出视频文件 + valid_extensions = (".mp4", ".avi", ".mov", ".mkv") + valid_files = [f for f in files if f.lower().endswith(valid_extensions)] + + if valid_files: + self.files_list.delete(0, tk.END) + for file in valid_files: + self.files_list.insert(tk.END, file) + else: + messagebox.showwarning( + "警告", "请拖入视频文件(支持mp4, avi, mov, mkv格式)" + ) # 添加拖放处理方法 def handle_drop(self, event): @@ -340,6 +386,31 @@ class VideoToGifConverter: self.file_frame = ttk.LabelFrame(self.root, text="选择文件") self.file_frame.pack(padx=10, pady=5, fill="x") + # 添加拖放提示框 + if SUPPORT_DND: + self.drop_frame = ttk.Frame(self.file_frame, style="Dropzone.TFrame") + self.drop_frame.pack(fill="both", expand=True, padx=10, pady=10) + + ttk.Label(self.drop_frame, text="拖放视频文件到这里", font=("", 12)).pack( + pady=20 + ) + + # 添加拖放效果 + self.drop_frame.bind( + "", + lambda e: self.drop_frame.configure(style="DropzoneHover.TFrame"), + ) + self.drop_frame.bind( + "", lambda e: self.drop_frame.configure(style="Dropzone.TFrame") + ) + + self.files_list = tk.Listbox(self.file_frame, height=5) + self.files_list.pack(padx=5, pady=5, fill="x") + + # 如果是macOS,为Listbox添加拖放支持 + if SUPPORT_DND == "macos": + self.files_list.bind("<>", self.handle_macos_drop) + self.files_list = tk.Listbox(self.file_frame, height=5) self.files_list.pack(padx=5, pady=5, fill="x") diff --git a/requirements.txt b/requirements.txt index 99ab930..9231889 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ # 核心依赖 -ffmpeg-python>=0.2.0 # FFmpeg Python绑定 -Pillow>=9.0.0 # 图像处理库 +ffmpeg-python>=0.2.0 +Pillow>=9.0.0 -# GUI增强 -tkinterdnd2>=0.4.2 # 修改为最新的可用版本 \ No newline at end of file +# Windows平台依赖 +tkinterdnd2>=0.4.2; platform_system=="Windows" +# Mac平台可以使用其他方案,暂时不需要特殊依赖