Replies: 3 comments 7 replies
-
Ways to implement the drag and drop file functionality in customtkinter. Method 1FIrst 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() Method 2Using pywinstyles (only works in windows), the most simple way def drop_func(file):
print(file)
pywinstyles.apply_dnd(widget, drop_func) Method 3This is also using tkinterdnd2: copy the customtkinter folder in the directory where your program is present, browse to Then do these changes in the ctk_tk.py file of customtkinter library.
Done! Now you can implement this drag and drop feature in your application. import customtkinter
from tkinterdnd2 import DND_FILES
def drop(event):
# do operations with event.data file
label.configure(text=event.data)
root = customtkinter.CTk()
root.geometry("500x200")
label = customtkinter.CTkLabel(root, text="➕ \nDrag & Drop Here", corner_radius=10, fg_color="blue", wraplength=300)
label.pack(expand=True, fill="both", padx=40, pady=40)
# Add this 2 lines to make it a dnd widget
label.drop_target_register(DND_FILES)
label.dnd_bind('<<Drop>>', drop)
root.mainloop() |
Beta Was this translation helpful? Give feedback.
-
If you don't want to change the files, try this: https://stackoverflow.com/a/75527642 Now I think this is the better method. ☝️ |
Beta Was this translation helpful? Give feedback.
-
🚀 Looking to create stunning Python GUIs with ease? Check out Buildfy, a powerful drag-and-drop GUI builder for Python that integrates seamlessly with CustomTkinter! 🎨 With Buildfy, you can: Quickly design modern, sleek interfaces without writing a single line of code #Python #PythonGUI #Buildfy #Tkinter #AI #AppDevelopment |
Beta Was this translation helpful? Give feedback.
-
Can we implement the drag and drop file feature in widgets like TkinterDND?
Or is there any other way to use customtkinter with TkinterDND?
Beta Was this translation helpful? Give feedback.
All reactions