Python GTK + 3 TextView无法序列化(或反序列化?)富文本标签

时间:2018-12-17 21:51:56

标签: python python-3.x gtk gtk3

我目前正在使用python-gi编写GTK + 3应用程序,而我一直在遇到一个简单的问题,即序列化TextBuffer,将其写入磁盘然后从磁盘加载它这一简单任务。

截至目前,我已经能够对纯文本进行序列化和反序列化,但是它不保留在输入中输入的任何富文本标签。

例如,如果我在应用程序中输入文本“ Hello!”,则在序列化然后反序列化之后,我会得到的只是“ Hello!”。

我根据文档编写了代码,并对照我在网上可以找到的每个示例对其进行了验证,但我真的看不出任何不可行的原因。

我有两个非常简单的函数来将TextBuffer保存到磁盘和从磁盘加载:

def save_buffer(self, buffer: Gtk.TextBuffer) -> str:
    """ Saves a TextBuffer to disk
    :param buffer: The buffer to be serialized
    :return: The name of the path written to
    """
    data = buffer.serialize(buffer, buffer.register_serialize_tagset(), *buffer.get_bounds())
    with open(self.path, 'wb') as file:
        file.write(data)
    return self.path

def load_buffer(self) -> Gtk.TextBuffer:
    """ Loads this Page's TextBuffer from disk
    :return: This Page's TextBuffer
    """
    buffer = Gtk.TextBuffer()
    tags = buffer.register_deserialize_tagset()
    with open(self.path, 'rb') as file:
        buffer.deserialize(buffer, tags, buffer.get_start_iter(), file.read())
    return buffer

0 个答案:

没有答案