Skip to content

Instantly share code, notes, and snippets.

@ssokolow
Created April 8, 2023 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssokolow/30e0cf931e80ab5a95ba1db9a7b23613 to your computer and use it in GitHub Desktop.
Save ssokolow/30e0cf931e80ab5a95ba1db9a7b23613 to your computer and use it in GitHub Desktop.
Quick and dirty example of receiving inter-application drag-and-drop in Python with GTK 3.x
#!/usr/bin/python
"""Quit and dirty port of the old PyGTK dragtargets.py to PyGObject"""
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gdk, Gtk
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):
targets = ""
for t in context.list_targets():
targets = targets + str(t) + "\n"
wid.drag_get_data(context, t, time)
print(targets)
label.set_text(targets)
#l.set_text('\n'.join([str(t) for t in context.targets]))
#print('\n'.join([str(t) for t in context.targets]))
#a = wid.drag_dest_find_target(context, t)
#context.finish(True, False, time)
return True
def drag_received_cb(widget, drag_context, x, y, selection_data, info, time):
print("Target: {}".format(selection_data.get_target()))
print("Data: {}".format(selection_data.get_data()))
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('drag_data_received', drag_received_cb)
w.connect('destroy', lambda w: Gtk.main_quit())
label = Gtk.Label()
w.add(label)
w.show_all()
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment