Python GTK Drag and Drop - Get URL

I am creating a small application that should be able to get URLs. If the application window is open, I have to drag the link from the browser and transfer it to the application - and the application will save the URL in the database.

I create this in Python / GTk. But I'm a little confused about drag and drop functionality. So how to do this?

Sample code to implement drag and drop (my application uses a bit of this code) ...

import pygtk pygtk.require('2.0') import gtk # function to print out the mime type of the drop item def drop_cb(wid, context, x, y, time): l.set_text('\n'.join([str(t) for t in context.targets])) # What should I put here to get the URL of the link? context.finish(True, False, time) return True # Create a GTK window and Label, and hook up # drag n drop signal handlers to the window w = gtk.Window() w.set_size_request(200, 150) w.drag_dest_set(0, [], 0) w.connect('drag_drop', drop_cb) w.connect('destroy', lambda w: gtk.main_quit()) l = gtk.Label() w.add(l) w.show_all() # Start the program gtk.main() 
+4
source share
4 answers

You must get the data yourself. Here is a simple working example that will label the url:

 #!/usr/local/env python import pygtk pygtk.require('2.0') import gtk def motion_cb(wid, context, x, y, time): l.set_text('\n'.join([str(t) for t in context.targets])) context.drag_status(gtk.gdk.ACTION_COPY, time) # Returning True which means "I accept this data". return True def drop_cb(wid, context, x, y, time): # Some data was dropped, get the data wid.drag_get_data(context, context.targets[-1], time) return True def got_data_cb(wid, context, x, y, data, info, time): # Got data. l.set_text(data.get_text()) context.finish(True, False, time) 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', got_data_cb) w.connect('destroy', lambda w: gtk.main_quit()) l = gtk.Label() w.add(l) w.show_all() gtk.main() 
+8
source

To get only the data of a single file or directory in the DnD'ing file of the file list from your file explorer, you can use something like:

 data.get_text().split(None,1)[0] 

The code for the got_data_cb method will look like this:

 def got_data_cb(wid, context, x, y, data, info, time): # Got data. l.set_text(data.get_text().split(None,1)[0]) context.finish(True, False, time) 

This would split the data into any spaces and return you the first element.

+3
source

The only solution for me:

 def got_data_cb(wid, context, x, y, data, info, time): # Got data. l.set_text(data.get_uris()[0]) context.finish(True, False, time) 
+1
source

The following code is ported from an example of the (old) PyGTK manual , which, it seems to me, inspired the accepted answer , but with pygi:

 #!/usr/local/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() 
0
source

All Articles