我創建一個小應用程序必須能夠接收URL。如果應用程序窗口是打開的,我應該能夠從瀏覽器中拖動鏈接並將其放入應用程序 - 應用程序會將URL保存到數據庫。Python GTK拖放 - 獲取URL
我在Python/GTk中創建了這個。但是我對它的拖放功能有些困惑。那麼,它是如何呢?
一些示例代碼來實現拖/放(我的應用程序,使用比特這段代碼的)...
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()
是,你可能要小心如果數據是以uri列表的形式調用data.get_uris()。例如,如果你是從konqueror/nautilus到窗口的文件列表中,並且接受了'text/uri-list'的話,GtkSelectionData上的get_data()將返回None。 – 2009-08-31 21:41:11