我正在尝试使用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()
相关链接:
答案 0 :(得分:1)
显然,'设置'期待一个字节串。
将行更改为:
selection.set(selection.get_target(), 8, data.encode())
似乎工作正常。我在这里找到了这个解决方案:
Gtk.CssProvider.load_from_data TypeError: Item 0: Must be number, not str