我希望源也是目标。我实现了下面的信号,但拖动数据获取信号触发了两次。第二次使用自动选择的ListStore项填充数据变量(在on_drag_data_get中(。
class DragSource(Gtk.TreeView):
def __init__(self):
Gtk.TreeView.__init__(self)
model = Gtk.ListStore(str)
self.set_model(model)
self.add_item("Item 1")
self.add_item("Item 2")
self.add_item("Item 3")
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn("Backlog", renderer, text=0)
self.append_column(column)
self.enable_model_drag_source(
Gdk.ModifierType.BUTTON1_MASK, [], DRAG_ACTION)
self.drag_dest_set(Gtk.DestDefaults.ALL, [], DRAG_ACTION)
self.connect("drag-data-get", self.on_drag_data_get)
self.connect("drag-data-received", self.on_drag_data_received)
def on_drag_data_get(self, widget, drag_context, data, info, time):
print("fired")
model, iter = self.get_selection().get_selected()
text = model.get_value(iter, 0)
data.set_text(text, -1)
def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
text = data.get_text()
self.add_item(text)
print("Received text: %s" % text)
def add_item(self, text):
self.get_model().append([text])
替换
self.drag_dest_set(Gtk.DestDefaults.ALL, [], DRAG_ACTION)
带有
dnd_list = [Gtk.TargetEntry.new("text/uri-list", 0, 80)]
self.drag_dest_set_target_list(dnd_list)
我遇到了同样的问题,信号处理程序将被调用两次,
要解决此问题,您必须通过从回调中返回True来停止事件传播,例如on_drag_data_get和on_drag_data_received
最小展示案例:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
def on_click(*args, **kwargs):
print('clicked')
return True # to prevent event from propagation and stop event from being fired twice
win = Gtk.Window()
win.connect('button-press-event', on_click)
win.show_all()
Gtk.main()
此评论归功于