python - pygobject 中的拖放文件示例

标签 python pygtk pygobject

我正在尝试将示例拖放示例从 pygtk FAQ 移植到 pygobject。

from gi.repository import Gtk as gtk
import urllib
import os

TARGET_TYPE_URI_LIST = 80
dnd_list = [ ( 'text/uri-list', 0, TARGET_TYPE_URI_LIST ) ]

def get_file_path_from_dnd_dropped_uri(uri):
    # get the path to file
    path = ""
    if uri.startswith('file:\\\\\\'): # windows
        path = uri[8:] # 8 is len('file:///')
    elif uri.startswith('file://'): # nautilus, rox
        path = uri[7:] # 7 is len('file://')
    elif uri.startswith('file:'): # xffm
        path = uri[5:] # 5 is len('file:')

    path = urllib.url2pathname(path) # escape special chars
    path = path.strip('\r\n\x00') # remove \r\n and NULL

    return path

def on_drag_data_received(widget, context, x, y, selection, target_type, timestamp):
    if target_type == TARGET_TYPE_URI_LIST:
        uri = selection.data.strip('\r\n\x00')
        print 'uri', uri
        uri_splitted = uri.split() # we may have more than one file dropped
        for uri in uri_splitted:
            path = get_file_path_from_dnd_dropped_uri(uri)
            print 'path to open', path
            if os.path.isfile(path): # is it file?
                data = file(path).read()
                #print data

w = gtk.Window()
w.connect('drag_data_received', on_drag_data_received)
w.drag_dest_set( gtk.DEST_DEFAULT_MOTION |
              gtk.DEST_DEFAULT_HIGHLIGHT | gtk.DEST_DEFAULT_DROP,
              dnd_list, gtk.gdk.ACTION_COPY)

w.show_all()
gtk.main()

有人可以帮我在 pygobject 上制作拖放示例吗?我在 windows7 上。

最佳答案

使用 pygobject 进行拖放的工作示例(在 Windows7 上测试)

from gi.repository import Gtk, GdkPixbuf, Gdk
import urllib
import os
TARGET_TYPE_URI_LIST = 80

def get_file_path_from_dnd_dropped_uri(uri):
    # get the path to file
    path = ""
    if uri.startswith('file:\\\\\\'): # windows
        path = uri[8:] # 8 is len('file:///')
    elif uri.startswith('file://'): # nautilus, rox
        path = uri[7:] # 7 is len('file://')
    elif uri.startswith('file:'): # xffm
        path = uri[5:] # 5 is len('file:')

    path = urllib.url2pathname(path) # escape special chars
    path = path.strip('\r\n\x00') # remove \r\n and NULL

    return path

def on_drag_data_received(widget, context, x, y, selection, target_type, timestamp):
    if target_type == TARGET_TYPE_URI_LIST:
        uri = selection.get_data().strip('\r\n\x00')
        print 'uri', uri
        uri_splitted = uri.split() # we may have more than one file dropped
        for uri in uri_splitted:
            path = get_file_path_from_dnd_dropped_uri(uri)
            print 'path to open', path
            if os.path.isfile(path): # is it file?
                data = file(path).read()
                #print data

w = Gtk.Window()
w.connect('drag_data_received', on_drag_data_received)

w.drag_dest_set( Gtk.DestDefaults.MOTION|
                  Gtk.DestDefaults.HIGHLIGHT | Gtk.DestDefaults.DROP,
                  [Gtk.TargetEntry.new("text/uri-list", 0, 80)], Gdk.DragAction.COPY)



w.show_all()
Gtk.main()

关于python - pygobject 中的拖放文件示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24094186/

相关文章:

python - 如何在 Windows 下将链接拖动到 PyGTK 应用程序?

python - keras-mxnet 无法从 pip 中正确拉取

python - 如何在 PyGTK 中设置默认按钮?

python - 显示大型弹出菜单的正确方法是什么?

python - PyGObject 模板子项未定义

python - pip3 install pygobject错误: Nothing to do, gio无法找到并且是必需的

python - 使用 virtualenv 为不同 python 版本安装 Egg 时出错

Python int 到二进制字符串?

python - 如何使用 python Gtk 和 webkit 在 webview 上叠加图像

python - 如何在 pygtk 中使用同一个小部件两次?