Drag and drop in Windows

Christian Gollwitzer auriocus at gmx.de
Mon Apr 29 16:38:29 EDT 2013


Hi Robert,

Am 29.04.13 12:25, schrieb Robert Flintham:
> I’ve found this (TkDND):
>
> http://wiki.tcl.tk/2768
 > But I don’t know how to implement this in Python.  The Windows binary
 > for it comes as a set of “.tcl” files and a single “.dll” file.
 > 2.direct implementation of the Tcl file [tk.eval(‘source …’)], but I
 > don’t reallu understand what’s going on with this – can you only execute
 > a “main” bit of Tcl files rather than implementing individual functions?

I can only comment on the Tcl side, since I'm not an expert in the 
Tkinter coupling mechanism. TkDND is indeed the way to go if you want 
native drag'n'drop support. The first step would indeed be to load the 
package into the Tcl interpreter. You need to:

1) Create a folder for the packages, put the files in a subfolder
Typically, this is something like lib/tkdnd, and at that level there 
must be the "pkgIndex.tcl" file
2) Append the lib/ folder to the auto path
tk.eval('lappend auto_path {mypath/lib}')
(the braces are Tcl's quoting mechanism)
3) load the package
tk.eval('package require tkdnd')

Then, you need to "register the target", i.e. declare a widget that it 
accepts files. Here, you need the Tk path name of the widget, which is 
retrieved by __str__:

tk.eval('tkdnd::drop_target register ' + yourwidget +' *')

Then, if you drop something, the widget recieves a virtual event 
<<Drop:DND_Files>> . Now this is tricky, I don't know how to bind to 
that event. Following the tutorial for Tcl on http://wiki.tcl.tk/36708, 
I suppose something like

	yourwidget.bind("<<Drop::DND_Files>>", filesdropped)

should in principle work, but how to get the data out of it? It is 
stuffed into the %D bind substitution. Usual events store the MouseWheel 
distance in this field; so maybe you can get it from the field 
event.delta. I can't test it now, but I am a bit skeptical whether this 
works with the guts of TkInter. If not, you'd need to do some more 
forwarding from the Tcl side.

	Christian



More information about the Python-list mailing list