TomSchimansky / CustomTkinter

A modern and customizable python UI-library based on Tkinter
MIT License
11.56k stars 1.08k forks source link

Drag And Drop #2506

Open Ricc4rdo0107 opened 4 months ago

Ricc4rdo0107 commented 4 months ago

Adding drag and drop bindings would not be bad for comletition, I'm making a file manager and I really need that

Akascape commented 3 months ago

@Ricc4rdo0107 There are two ways to implement file drag and drop feature in CTk.

Using TkDnD2 (For all platforms)

FIrst install tkinterdnd2:

pip install tkinterdnd2

Now use it in the code like this:

import customtkinter
from tkinterdnd2 import TkinterDnD, DND_ALL

class CTk(customtkinter.CTk, TkinterDnD.DnDWrapper):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.TkdndVersion = TkinterDnD._require(self)

def get_path(event):
    dropped_file = event.data.replace("{","").replace("}", "")
    # do further operation

root = CTk()
root.drop_target_register(DND_ALL)
root.dnd_bind("<<Drop>>", get_path)

root.mainloop()

Using pywinstyles (Only windows)

import pywinstyles
...
def drop_func(file):
  print(file)

pywinstyles.apply_dnd(widget, drop_func)
Ricc4rdo0107 commented 3 months ago

@Ricc4rdo0107 There are two ways to implement file drag and drop feature in CTk.

Using TkDnD2 (For all platforms)

FIrst install tkinterdnd2:

pip install tkinterdnd2

Now use it in the code like this:

import customtkinter
from tkinterdnd2 import TkinterDnD, DND_ALL

class CTk(customtkinter.CTk, TkinterDnD.DnDWrapper):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.TkdndVersion = TkinterDnD._require(self)

def get_path(event):
    dropped_file = event.data.replace("{","").replace("}", "")
    # do further operation

root = CTk()
root.drop_target_register(DND_ALL)
root.dnd_bind("<<Drop>>", get_path)

root.mainloop()

Using pywinstyles (Only windows)

import pywinstyles
...
def drop_func(file):
  print(file)

pywinstyles.apply_dnd(widget, drop_func)

Thank you very much, I actually alredy did the drop part using tkinterdnd2, but I didn't know about pywinstyles. The only problem is draggin for example a label with a filename, and moving tho other directories dropping it for example in the desktop, or dropping in the terminal so it displayies the full path, I tried doing it and the drag part works even out of the CTK Window but then when I drop nothing happens. Thank you for the help, it means a lot.