sebp / PyGObject-Tutorial

Tutorial for using GTK+ 3 in Python
https://python-gtk-3-tutorial.readthedocs.io/
GNU Lesser General Public License v2.1
393 stars 161 forks source link

include example of how to get the required TargetEntry for drag and drop #83

Open muelli opened 8 years ago

muelli commented 8 years ago

I wanted to build an app which accepts drags. I looked up the drag and drop tutorial but it wasn't clear to me how I would make my app accept drags from other applications. Also, I needed to produce the necessary TargetEntries which I didn't know how to do.

The following example is ported from the pygtk tutorial but uses pygi. Maybe it's possible to include that somehow in the current tutorial.

#!/usr/bin/env python
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk

def motion_cb(wid, context, x, y, time):
    Gdk.drag_status(context, Gdk.DragAction.COPY, time)
    return True

def drop_cb(wid, context, x, y, time):
    l.set_text('\n'.join([str(t) for t in context.list_targets()]))
    context.finish(True, False, time)
    return True

w = Gtk.Window()
w.set_size_request(200, 150)
w.drag_dest_set(0, [], 0)
w.connect('drag-motion', motion_cb)
w.connect('drag-drop', drop_cb)
w.connect('destroy', lambda w: Gtk.main_quit())
l = Gtk.Label()
w.add(l)
w.show_all()

Gtk.main()
sebp commented 7 years ago

If you provide a pull request with the example and some text explaining it, I'd be happy to add it.