【问题标题】:Python GTK Drag and Drop - Get URLPython GTK 拖放 - 获取 URL
【发布时间】:2010-11-16 05:35:09
【问题描述】:

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

【问题讨论】:

    标签: python drag-and-drop gtk gdk


    【解决方案1】:

    您必须自己获取数据。这是一个简单的工作示例,它将为删除的 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()
    

    【讨论】:

    • 请注意,如果数据是 uri 列表的形式,您可能必须调用 data.get_uris()。因此,例如,如果您将 konqueror/nautilus 中的文件列表复制到窗口并且您接受说“text/uri-list”,则 GtkSelectionData 上的 get_data() 将返回 None。
    • 如果我们拖到窗口上然后拖出窗口窗口而不释放保留文本的标签的鼠标,可能会出现不良行为。清除标签似乎更有意义
    【解决方案2】:

    为了确保只从文件资源管理器中获取文件列表中的一个文件或目录的数据,您可以使用以下内容:

    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)
    

    这将按任何空格分割数据并返回第一项。

    【讨论】:

      【解决方案3】:

      下面的代码是从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()
      

      【讨论】:

        【解决方案4】:

        对我有用的唯一解决方案是:

        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)
        

        【讨论】:

          猜你喜欢
          • 2012-04-29
          • 1970-01-01
          • 2014-04-12
          • 2012-04-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多