2009-08-02 96 views
4

我创建一个小应用程序必须能够接收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() 

回答

8

您必须自己获取数据。这里有一个简单的工作示例,将标签设置为网址下降:

#!/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

是,你可能要小心如果数据是以uri列表的形式调用data.get_uris()。例如,如果你是从konqueror/nautilus到窗口的文件列表中,并且接受了'text/uri-list'的话,GtkSelectionData上的get_data()将返回None。 – 2009-08-31 21:41:11

3

就一定要得到您的文件浏览器DnD'ing文件的列表中的一个文件或目录的只有数据,你可以使用类似:

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

了代号为“got_data_cb”方法则是这样的:

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) 

这将通过任何空格分割数据,并返回你的第一个项目。

1

工作对我来说唯一的解决办法是:

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) 
0

下面的代码是从an example of the (old) PyGTK tutorial我猜启发the accepted answer移植,但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()