[pygtk] pygtk Digest, Vol 73, Issue 15
Joaquin Sargiotto
joaquinsargiotto_2 at hotmail.com
Fri Mar 13 22:49:05 WST 2009
> Hi
>
> Does any one have any pointers on how to drag a file from nautilus and
> receive the info in a treeview widget?
>
> All the examples I google are about reordering iters, and for soem
> reason treeviews behave differently to other widgets for dnd.
I've made what you want, but I've only tested in windows, but for what I've search it should work with nautilus too. The example its made with a gtk.Window but works perfectly for a treeview (its working on my project so far)
<code>
import gtk
import urllib
import os
# Drag and drop attributes
TARGET_TYPE_URI_LIST = 80
dnd_list = [ ( 'text/uri-list', 0, TARGET_TYPE_URI_LIST ) ]
def get_file_path_from_dnd_dropped_uri(uri):
# get the path to file
path = ""
if uri.startswith('file:///'): # windows
path = uri[8:] # 8 is len('file:///')
elif uri.startswith('file://'): # nautilus, rox
path = uri[7:] # 7 is len('file://')
elif uri.startswith('file:'): # xffm
path = uri[5:] # 5 is len('file:')
path = urllib.url2pathname(uri) # escape special chars
path = path.strip('\r\n\x00') # remove \r\n and NULL
return path
def on_drag_data_received(widget, context, x, y, selection, target_type, timestamp):
if target_type == TARGET_TYPE_URI_LIST:
uri = selection.data.strip('\r\n\x00')
uri_splitted = uri.split() # we may have more than one file dropped
for uri in uri_splitted:
print(uri) # Prints each file (but with file:///)
w = gtk.Window()
w.connect('drag_data_received', on_drag_data_received)
w.drag_dest_set( gtk.DEST_DEFAULT_MOTION |
gtk.DEST_DEFAULT_HIGHLIGHT | gtk.DEST_DEFAULT_DROP,
dnd_list, gtk.gdk.ACTION_COPY)
w.show_all()
gtk.main()
</code>
(I'm sorry, you'll have to fix the indentation)
What i'm still not sure about is get_file_path_from_dnd_dropped_uri, because i've test the program on gnome and vista and both returns file:///{uri}, but according to this function (I didn't made it) the string should be different, but its not. So give it a try.
Best regars, Joaquín.
_________________________________________________________________
Encontrá el auto de tus sueños en MSN
http://xml.mercadolibre.com.ar/org-img/msn/autos.html
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: http://www.daa.com.au/pipermail/pygtk/attachments/20090313/4d3a8158/attachment.htm
------------ próxima parte ------------
Se ha borrado un mensaje que no est? en formato texto plano...
Nombre : dragndrop.py
Tipo : application/octet-stream
Tama?o : 1354 bytes
Descripci?n: no disponible
Url : http://www.daa.com.au/pipermail/pygtk/attachments/20090313/4d3a8158/attachment.obj
More information about the pygtk
mailing list