2009-08-02 9 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에서 창까지의 파일 목록을 DnD하면 'text/uri-list'라고 말하면 GtkSelectionData의 get_data()는 None을 반환합니다. –

+0

윈도우를 드래그하여 텍스트를 유지하는 라벨의 마우스를 놓지 않고 윈도우 창 밖으로 드래그하면 바람직하지 않은 동작이 발생할 수 있습니다. 라벨을 지우는 것이 더 합리적인 것 같습니다. – demongolem

3

하는 파일 탐색기에서 파일 목록을 DnD'ing에 하나 개의 파일이나 디렉토리의 데이터를 얻을 확인하기 위해 당신이 할 수, 다음은 떨어 URL에 레이블을 설정하는 간단한 동작하는 예제입니다 다음과 같이 할 것 "got_data_cb"방법에 대한

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

코드 : 같은 것을 사용이 공백으로 데이터를 분할하고 당신에게 첫 번째 항목을 반환 할

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

다음 코드는 내가 the accepted answer 영감을 추측 an example of the (old) PyGTK tutorial에서 포팅하지만 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()