我使用Gtk.Entry函数创建了一个Drag and Drop。该条目能够从拖放中接收文本数据。 但是不允许手动更改文本,因此我将editable设置为False。
现在,拖放操作不再起作用。不再调用方法“ on_drag_data_received”。 正在寻找解决方案...
class DragDropEntry(Gtk.Entry):
def __init__(self, placeholder_text="drop something here", editable=False):
Gtk.Entry.__init__(self, placeholder_text=placeholder_text, editable=editable)
self.drag_dest_set(Gtk.DestDefaults.ALL, [], DRAG_ACTION)
self.connect("drag-data-received", self.on_drag_data_received)
self.drag_dest_add_text_targets()
def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
uri = data.get_data().strip('\r\n\x00')
uri_splitted = uri.split() # we may have more than one file dropped
for uri in uri_splitted:
path = self.get_file_path_from_dnd_dropped_uri(uri)
self.set_text(path)
return
def get_file_path_from_dnd_dropped_uri(self, uri):
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