TextBuffer 序列化问题

TextBuffer 序列化问题

在尝试遵循序列化说明时这个答案我遇到了一些问题。

下面的代码应该将 TextArea 的内容序列化为字符串。前两行完成得很好,但是从那以后我就遇到了问题。

page_content = subject_content.get_nth_page(pn) //TextArea widget returned
page_name = subject_content.get_tab_label(page_content).get_text()
c_buffer = page_content.get_buffer()
c_format = c_buffer.register_serialize_tagset()
serial = c_buffer.serialize(c_format, c_buffer.get_start_iter(), c_buffer.get_end_iter())

我收到的第一个错误是:

Traceback (most recent call last):
  File "/home/nja/notetaker/notetaker/NotetakerWindow.py", line 251, in on_btn_save_clicked
    self.save()
  File "/home/nja/notetaker/notetaker/NotetakerWindow.py", line 160, in save
    c_format = c_buffer.register_serialize_tagset()
  File "/usr/lib/python2.7/dist-packages/gi/types.py", line 43, in function
    return info.invoke(*args, **kwargs)
TypeError: register_serialize_tagset() takes exactly 2 arguments (1 given)

None作为参数插入到c_format = c_buffer.register_serialize_tagset() 该行时完成,但下一行不起作用:

Traceback (most recent call last):
  File "/home/nja/notetaker/notetaker/NotetakerWindow.py", line 251, in on_btn_save_clicked
    self.save()
  File "/home/nja/notetaker/notetaker/NotetakerWindow.py", line 161, in save
    serial = c_buffer.serialize(c_format, c_buffer.get_start_iter(), c_buffer.get_end_iter())
  File "/usr/lib/python2.7/dist-packages/gi/types.py", line 43, in function
    return info.invoke(*args, **kwargs)
TypeError: serialize() takes exactly 5 arguments (4 given)

None我不知道该如何解决这个问题,但是我首先不应该填写其他功能。

这里发生了什么事?

答案1

Gtk 文档说 c_buffer.register_serialize_tagset() 需要一个 utf8 字符串作为参数。

看起来你需要:
c_buffer.register_serialize_tagset(c_buffer.get_text().encode('utf-8'))
或类似的东西

这有效:

from gi.repository import Gtk

win = Gtk.Window()
box = Gtk.Box()
win.add(box)

text = Gtk.TextView()
box.add(text)

buffer = text.get_buffer()

buffer.set_text("Sample text")

this = buffer.get_text(buffer.get_start_iter(), buffer.get_end_iter(), False)

format = buffer.register_serialize_tagset(this)

data = buffer.serialize(buffer, format, buffer.get_start_iter(), buffer.get_end_iter())

print data

Gtk.main()

相关内容