tkdnd icon indicating copy to clipboard operation
tkdnd copied to clipboard

event.data format sucks

Open AzazelHD opened this issue 2 years ago • 6 comments

Hello.

When you drag, lets say 3 files into the TkinterDnD.Tk() window, u get their path, separate with whitespaces.

So what happens if a path contains whitespaces? It is a mess.

Can you separate paths with any other character? like semicolon or something? Or, ideally, return the data on a list, not on a single string.

If no, can u at least tell me what should i modify in order to achieve this?

thanks in advance

AzazelHD avatar Mar 22 '23 01:03 AzazelHD

I am not sure what you mean. During drops, data are passed as a Tcl list. For example:

https://github.com/petasis/tkdnd/blob/master/library/tkdnd_generic.tcl#L388

You are probably referring to python's tkinter. Maybe it is not handled correctly there?

petasis avatar Mar 24 '23 09:03 petasis

Im using tkinterdnd2. Python tkinter doesnt even support dropping files.

import tkinter as tk
from tkinterdnd2 import DND_FILES, TkinterDnD

root = TkinterDnD.Tk()

def handle_drop(event):
    paths = event.data.strip()
    print(paths)


root.geometry("640x400")
root.title("Drag and Drop Example")

drop_zone = tk.Label(root, text="Drop files here", font=("Arial", 30))
drop_zone.pack(expand=True, fill=tk.BOTH)

# register the listbox as a drop target
drop_zone.drop_target_register(DND_FILES)
drop_zone.dnd_bind('<<Drop>>', handle_drop)


root.mainloop()

paths looks like "E:/path with spaces/file1.txt E:/path2/normal/vid.mp4". But on a single string

AzazelHD avatar Mar 24 '23 21:03 AzazelHD

In a later version of tkinterdnd2 for python, this was made even stranger: paths without spaces look like normal and are separated from the next with a space, but paths WITH spaces are surrounded by braces. So you get a string back that looks like

"E:/simple/path.txt {E:/complicated path/oh no.txt} E:/simple/this_is_weird.txt"

~~Is there some existing python function for converting such 'stringified' TCL list into a regular python list?~~

Wow, found something in the tkinterdnd2 demos: calling Tk's splitlist() on the event data should work. Something like:

listbox.tk.splitlist(event.data)   # from the demo or...
root.tk.splitlist(event.data)      # to not limit ourselves to listbox

johnwoltman avatar Jan 17 '24 16:01 johnwoltman

I dont know, maybe it is me, but i dont think storing paths on a list is too hard/revolutionary wtf????

AzazelHD avatar Jan 17 '24 23:01 AzazelHD

I dont know, maybe it is me, but i dont think storing paths on a list is too hard/revolutionary wtf????

I agree, and it would be nice if event.data was a list instead of a weird string. It's not too bad having to call the splitlist() method myself I guess. The bigger problem is that it would be nice to know that splitlist even exists if you (like me) don't know anything about Tcl/Tk under the hood.

I looked at the underlying Tcl stuff in tkinterdnd2 and I do not want to learn Tcl, but I might poke around and see what it would take to add the stuff I want to tkinterdnd2.

Sorry, @petasis, for chiming in on your project's bug tracker when it seems more related to tkinterdnd2.

johnwoltman avatar Jan 17 '24 23:01 johnwoltman