TreeView 拖放:发送字符串接收字节

TreeView drag and drop: Sending string receiving bytes

我正在尝试使用 JSON 字符串在 Gtk TreeView 中实现拖放操作以序列化(我用的那个词对吗?)信息。但是当我使用标准方法时,我收到一条错误消息。由于某种原因,我发送的字符串也以字节形式到达。

发送函数为:

def drag_data_get(self, treeview, context, selection, info, time):
    treeselection = treeview.get_selection()
    model, itr = treeselection.get_selected()
    row = model[itr]
    data = json.dumps([row[0], row[1]])
    print(type(data), data)
    selection.set(selection.get_target(), 8, data)

接收函数为:

def drag_data_received(self, treeview, context, x, y, selection, info, time):
    drop_info = self.view.get_dest_row_at_pos(x, y)
    data = selection.get_data()
    print(type(data), data)

错误发生在drag_data_get()函数的最后一个ro:

Traceback (most recent call last):
  File "tv_dnd_json.py", line 63, in drag_data_get
    one = selection.set(selection.get_target(), 8, data)
TypeError: Item 0: Must be number, not str

我不知道如何解释这个回溯。

整个独立的例子:

from gi.repository import Gtk, Gdk
import json

class MainWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="TreeView Drag and Drop")
        self.connect("delete-event", Gtk.main_quit)
        self.set_border_width(10)
        self.set_default_size(400, 300)
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        self.add(vbox)

        self.store = Gtk.TreeStore(bool, str)
        self.view = Gtk.TreeView(model=self.store)
        vbox.add(self.view)

        renderer_toggle = Gtk.CellRendererToggle()
        column_toggle = Gtk.TreeViewColumn("", renderer_toggle, active=0)
        renderer_toggle.connect("toggled", self.on_toggled)
        self.view.append_column(column_toggle)

        renderer_name = Gtk.CellRendererText()
        column_name = Gtk.TreeViewColumn("Name", renderer_name, text=1)
        self.view.append_column(column_name)

        self.view.connect("drag-begin", self.drag_begin)
        self.view.connect("drag-data-get", self.drag_data_get)
        self.view.connect("drag-drop", self.drag_drop)
        self.view.connect("drag-data-delete", self.drag_data_delete)
        self.view.connect("drag-data-received", self.drag_data_received)
        self.view.connect("drag-end", self.drag_end)

        targets = [("text/plain", 0, 0)]
        self.view.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK,
            targets, Gdk.DragAction.DEFAULT|Gdk.DragAction.MOVE)
        self.view.enable_model_drag_dest(targets, Gdk.DragAction.DEFAULT)

        self.add_test_data()

    def add_test_data(self):
        parent = self.store.append(None, [True, "Item 1"])
        self.store.append(parent, [True, "Item 2"])
        self.store.append(None, [True, "Item 3"])
        self.store.append(None, [True, "Item 4"])

    def on_toggled(self, cellrenderer, path):
        self.store[path][0] = not self.store[path][0]

    def drag_begin(self, treeview, context):
        print("===================")
        print("Drag started")

    def drag_data_get(self, treeview, context, selection, info, time):
        print("===================")
        print("Drag data requested by destination")
        print("Selection", selection)
        treeselection = treeview.get_selection()
        model, itr = treeselection.get_selected()
        row = model[itr]
        data = json.dumps([row[0], row[1]])
        print(type(data), data)
        selection.set(selection.get_target(), 8, data)

    def drag_drop(self, treeview, context, selection, info, time):
        print("===================")
        print("Drag data droped")

    def drag_data_received(self, treeview, context, x, y, selection, info, time):
        print("===================")
        print("Drag data received")
        drop_info = self.view.get_dest_row_at_pos(x, y)
        data = selection.get_data()
        print(type(data), data)

    def drag_end(self, treeview, context):
        print("===================")
        print("Drag data end")

    def drag_data_delete(self, treeview, context):
        print("===================")
        print("Drag data delete")

win = MainWindow()
win.show_all()
Gtk.main()

相关链接:

显然,'set' 需要一个字节字符串。

将行更改为:

    selection.set(selection.get_target(), 8, data.encode())

似乎工作正常。我在这里找到了这个解决方案:

Gtk.CssProvider.load_from_data TypeError: Item 0: Must be number, not str