我无法序列化包含锚点的 TextBuffer。我希望将文本序列化到每个锚点,跳过锚点并重复。但是缓冲区中存在锚点会阻止序列化,即使锚点不在序列化范围内。这是一个错误还是我做错了什么?或者,如果我删除它的锚点。有没有办法创建 TextBuffer 的副本?使用 copy.copy 我得到“TypeError:gobject.GObject 后代的实例是不可复制的”
这是一个有效的测试代码:
import gtk
window = gtk.Window()
window.set_default_size(300, 300)
window.connect("destroy", lambda w: gtk.main_quit())
vb = gtk.VBox()
window.add(vb)
tv1 = gtk.TextView()
tb1 = tv1.get_buffer()
vb.pack_start(tv1)
def on_bt1_clicked(button, ):
""" Case1: We delete anchor... it is possible to serialize buffer range."""
serialFormat = tb1.register_serialize_tagset()
print("Button 1 was clicked")
iter_at_anchor = tb1.get_iter_at_child_anchor(anchor1)
iter_at_anchor.forward_char()
tb1.backspace(iter=iter_at_anchor, interactive=True, default_editable=True)
tb1.serialize(tb1, serialFormat, tb1.get_start_iter(), iter_at_anchor)
print("serialize was sucessful")
bt1 = gtk.Button("click me 1")
bt1.connect("clicked", on_bt1_clicked)
txt = "Some not so random text."
iter=tb1.get_start_iter()
tb1.insert(iter=iter, text=txt)
iter.set_offset(char_offset=10)
anchor1 = tb1.create_child_anchor(iter)
tv1.add_child_at_anchor(bt1, anchor1)
tv2 = gtk.TextView()
tb2 = tv2.get_buffer()
vb.pack_start(tv2)
def on_bt2_clicked(button, ):
""" Case2: We leave anchor but don't include it in range to be
serialized... it is not possible to serialize buffer range as process
freezes."""
serialFormat = tb2.register_serialize_tagset()
print("Button 2 was clicked")
iter_at_anchor = tb2.get_iter_at_child_anchor(anchor2)
iter_at_anchor.backward_char()
tb2.serialize(tb2, serialFormat, tb2.get_start_iter(), iter_at_anchor)
print("serialize was sucessful") # does not reach here
bt2 = gtk.Button("click me 2")
bt2.connect("clicked", on_bt2_clicked)
txt = "Some not so random text."
iter=tb2.get_start_iter()
tb2.insert(iter=iter, text=txt)
iter.set_offset(char_offset=10)
anchor2 = tb2.create_child_anchor(iter)
tv2.add_child_at_anchor(bt2, anchor2)
window.show_all()
gtk.main()
感谢您的任何意见