Drag and drop in Windows

Robert Flintham Robert.Flintham at uhb.nhs.uk
Tue Apr 30 04:39:43 EDT 2013


Thanks Christian.

I've tried the following code:
################
import Tkinter

root = Tkinter.Tk()
root.title("DICOM Opener")
root.tk.eval('lappend auto_path {K:/Python27/Lib/site-packages/tkdnd2.6}')
root.tk.eval('package require tkdnd')
################

But I get the following traceback:
################
Traceback (most recent call last):
  File "K:\eclipse\plugins\org.python.pydev_2.7.1.2012100913\pysrc\pydevd.py", line 1397, in <module>
    debugger.run(setup['file'], None, None)
  File "K:\eclipse\plugins\org.python.pydev_2.7.1.2012100913\pysrc\pydevd.py", line 1090, in run
    pydev_imports.execfile(file, globals, locals) #execute the script
  File "K:\PROGRAMMING\mripy\dicom_opener.py", line 7, in <module>
    root.tk.eval('package require tkdnd')
_tkinter.TclError: couldn't load library "K:/Python27/Lib/tkdnd2.6/tkdnd26.dll": invalid argument
################

Is "invalid argument" as generic as it sounds, or does it mean something specific in this case?  Is Tcl expecting an additional argument in the 'package require' line?

I assume the append auto path line is correct as it seems to have found the DLL (presumably from pkgIndex.tcl).

Apologies for any disclaimers that pad out this message - it's a work email account and they get added after I hit "send".

All the best,
Rob

-----Original Message-----
From: Python-list [mailto:python-list-bounces+robert.flintham=uhb.nhs.uk at python.org] On Behalf Of Christian Gollwitzer
Sent: 29 April 2013 21:38
To: python-list at python.org
Subject: Re: Drag and drop in Windows

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
--
http://mail.python.org/mailman/listinfo/python-list

DISCLAIMER:
This email and any attachments hereto contains proprietary information, some or all of which may be confidential or legally privileged. It is for the exclusive use of the intended recipient(s) only. If an addressing or transmission error has misdirected this e-mail and you are not the intended recipient(s), please notify the author by replying to this e-mail. If you are not the intended recipient you must not use, disclose, distribute, copy, print, or rely on this e-mail or any attachments, as this may be unlawful.




More information about the Python-list mailing list