PyGObject 拖放重新排序小部件

PyGObject drag and drop reorder widgets

我正在尝试使用 Python 在 Gtk3 中实现 DND,但到目前为止我只设法捕捉到 drag-begin 信号。这是代码:

class MyWidget(Gtk.EventBox):
    def __init__(self):
        super(TrackBox, self).__init__()

        self.drag_source_set(Gdk.ModifierType.BUTTON1_MASK, [], Gdk.DragAction.MOVE)
        self.connect('drag-begin', self._sig_drag_begin)
        self.connect('drag-data-get', self._sig_drag_data_get)

        self.drag_dest_set(Gtk.DestDefaults.HIGHLIGHT | Gtk.DestDefaults.DROP | Gtk.DestDefaults.MOTION, [Gtk.TargetEntry.new('GTK_MYWIDGET', Gtk.TargetFlags.SAME_APP, 0)], Gdk.DragAction.MOVE)
        self.connect('drag-drop', self._sig_drag_drop)
        self.connect('drag-data-received', self._sig_drag_data_received)

    def _sig_drag_begin(self, widget, context):
        print("drag begin")
        print(self, widget, context)

    def _sig_drag_data_get(self, widget, context, selection, info, timestamp):
        print("drag data get")
        print(self, widget, context, selection, info, timestamp)

    def _sig_drag_drop(self, widget, context, x, y, timestamp):
        print('drag-drop')
        print(self, widget, context, x, y, timestamp)

    def _sig_drag_data_received(self, widget, context, x, y, selection, info, timestamp):
        print('drag-data-received')
        print(self, widget, context, x, y, selection, info, timestamp)

我得到了 _sig_drag_begin 的输出,但没有别的。目标是能够在 Gtk.Box 中对这些 Gtk.EventBox-es 进行重新排序,最终结果与我们通过可重新排序的 Gtk.TreeView.

得到的结果有些相似。

如果有人有同样的问题,这里是解决方案:

class TrackBox(Gtk.EventBox):
    def __init__(self, title, artist, id, year, length, album):
        super(TrackBox, self).__init__()
        
        # do your stuff here

        # Looks like this one is needed
        targs = [Gtk.TargetEntry.new("dummy", Gtk.TargetFlags.SAME_APP, 1)]

        self.drag_source_set(Gdk.ModifierType.BUTTON1_MASK, targs, Gdk.DragAction.MOVE)

        self.drag_dest_set(Gtk.DestDefaults.HIGHLIGHT | Gtk.DestDefaults.DROP | Gtk.DestDefaults.MOTION, targs, Gdk.DragAction.MOVE)
        self.connect('drag-drop', self._sig_drag_drop)
        self.connect('drag-end', self._sig_drag_end)

    def _sig_drag_drop(self, widget, *_):
        # Here you get the name of the target
        print('drag-drop: get target name')
        print(widget.get_name())

    def _sig_drag_end(self, widget, _):
        # And here you get the name of the source
        # also you should write moving related code here
        print('drag-end: actually move and get source name')
        print(widget.get_name())

This视频详细解释了一切。