簡體   English   中英

Python GTK拖放 - 獲取URL

[英]Python GTK Drag and Drop - Get URL

我正在創建一個小應用程序必須能夠接收URL。 如果應用程序窗口打開,我應該可以從瀏覽器拖動鏈接並將其放入應用程序 - 應用程序將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()

您必須自己獲取數據。 這是一個簡單的工作示例,它將為丟棄的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()

為了確保只從DnD獲取文件資源管理器中的文件列表中的一個文件或目錄的數據,您可以使用以下內容:

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)

這將按任何空格分割數據並返回第一個項目。

唯一適合我的解決方案是:

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)

以下代碼是從(舊)PyGTK教程的示例移植而來 ,我猜這個 教程啟發了接受的答案 ,但是使用了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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM