TkinterDnD

TkinterDnD is a python wrapper for George Petasis' tkDnD Tk extension.
tkDnD adds native drag and drop support for windows and unix to Tk. TkinterDnD makes this functionality available for Tkinter.
If you want to use TkinterDnD you will of course have to install tkDnD first. Windows and linux binaries as well as the sources can be found here .

Usage

The usage can be as simple as:
    from Tkinter import *
    from TkinterDnD import *
    root = TkinterDnD.Tk()
This loads the tkdnd Tk library and adds the dnd methods to all Tkinter widgets.

Now let's add a listbox to our window as a drop target:
    lbox = Listbox(root)
    lbox.pack(fill=BOTH, expand=1)
    root.update()# may be necessary on unix systems
In order to make the listbox a decent drop target we will have to define callbacks for the <Drag> <DragEnter> and <Drop> events:
    def drag(event):
        return event.action

    def drag_enter(event):
        event.widget.focus_force()
        return event.action

    def drop(event):
        if event.type == "text/uri-list':
            files = event.data.split("file:")
            for f in files:
                if f: event.widget.insert(END, f.strip())
Finally we have to register the listbox as a drop target for the dnd events:
    lbox.bindtarget("text/uri-list", "<Drag>", drag)
    lbox.bindtarget("text/uri-list", "<DragEnter>", drag_enter)
    lbox.bindtarget("text/uri-list", "<Drop>", drop)
Now we should be ready to drop files from any file manager into the listbox.

Documentation

There is a reference manual and information you don't find there should be provided by the tkDND and shape man pages (all included in the download).

News

July 07 2005 : initial release

Dec. 08 2005: v.0.4

Sorry, the interface changed more or less completely. I decided however that the improvements I made are worth it.
There is now no more need to manually pass percent substitutions to the dnd callbacks; these are now handled by event instances as in Tkinter (see example code above).
The Tix interface is now handled by a separate class TkinterDnD.TixTk().
There are new methods that allow to use canvas items as drop targets and the new askselectedaction() method allows to let the user decide which action should actually be performed on drop events.
The TkinterDnD module is now located inside a separate TkinterDnD directory; the main reason for this is that it allows to safely import TkinterDnD with from TkinterDnD import * and to add a bunch of useful constants to the global namespace this way, as in Tkinter.


Get TkinterDnD

Here you can download the latest version of TkinterDnD (docs included in the zip archive).
Any feedback is much appreciated. Send bug reports and comments to:

Michael Lange <klappnase (at) freakmail (dot) de>

License

TkinterDnD is released in the public domain.

(back to main page)