"I have a simple mission: To create an open-source, non-linear video editor for Linux. Many have tried and fallen before me, but for some reason I feel compelled to try myself. I am documenting my journey in this blog for all to read. It will be a dangerous journey, and I might not make it back alive. Hold on tight, and enjoy the ride! By the way, I'm calling this project OpenShot Video Editor!"

If you have ever attempted to enable drag & drop with Python and GTK, then you've probably run into the same problem I did. When I would drop an item on a widget, it just would not fire off the signal handler. Very frustrating!!!

However, after some digging around, I found the solution. It's a GTK method that you must call to enable a widget as a drop target. The method is widgetname.drag_dest_set(). See my example below to see how it's used.

Example Code:

This isn't a fully working example, but the code snippets are valid syntax and should be useful in your own program. Good luck!

# Set the type of file that can be dropped
TARGET_TYPE_URI_LIST = 80
dnd_list = [ ( 'text/uri-list', 0, TARGET_TYPE_URI_LIST ) ]

# Connect a function to handle the drop signal
myTree.connect('drag_data_received', on_drag_data_received)

# IMPORTANT: Set an object as a drop destination, or none of this code will work
myTree.drag_dest_set( gtk.DEST_DEFAULT_MOTION |
gtk.DEST_DEFAULT_HIGHLIGHT | gtk.DEST_DEFAULT_DROP,
dnd_list, gtk.gdk.ACTION_COPY)

# Connect a function to handle the drag motion signal from a goocanvas widget
MyCanvas.connect('drag_motion', on_drag_motion)

# Set the canvas to allow drops from any type of file
MyCanvas.drag_dest_set(0, [], 0)

# Function to handle the drag motion
def on_drag_motion(wid, context, x, y, time):
print " *** motion detected ***"

# Function to handle the drop
def on_drag_data_received(widget, context, x, y, selection, target_type, timestamp):
print " ((( drop detected ))) "

2 comments

  1. Fredrik  

    I would like to see this as a working example. Im trying create a dod for a threeview, but it dont works for me :(

    //Fredrik

  2. Jonathan Thomas  

    Fredrik,
    Check out my newest post on drag n drop, and it might help you out: http://myvideoeditor.blogspot.com/2008/10/gtk-python-and-dnd-revisited.html

    Thanks,
    -Jonathan

Post a Comment

Subscribe to: Post Comments (Atom)