需要有关 GtkTextBuffer(GtkTextView)序列化/反序列化的示例/帮助
我正在尝试将用户的粗体/斜体/字体/等标签保存在 GtkTextView 中。 使用 GtkTextBuffer.get_text() 不会返回标签。
我发现的最好的文档是: http://www.pygtk .org/docs/pygtk/class-gtktextbuffer.html#method-gtktextbuffer--register-serialize-format
但是,我不理解函数参数。 如果有一个示例来说明如何使用它们来保存/加载带有标签的文本视图,那将会非常方便。
编辑:我想澄清一下我想要完成的任务。基本上我想保存/加载文本视图的文本+标签。我不想做比这更复杂的事情。我使用 pickle 作为文件格式,所以我不需要任何关于如何保存它或以什么格式保存它的帮助。只需要一种拉/推数据的方法,以便用户不会丢失他/她在屏幕上看到的任何内容。谢谢。
I am trying to save user's bold/italic/font/etc tags in a GtkTextView.
Using GtkTextBuffer.get_text() does not return the tags.
The best documentation I have found on this is:
http://www.pygtk.org/docs/pygtk/class-gtktextbuffer.html#method-gtktextbuffer--register-serialize-format
However, I do not understand the function arguments.
It would be infinitely handy to have an example of how these are used to save/load a textview with tags in it.
Edit: I would like to clarify what I am trying to accomplish. Basically I want to save/load the textview's text+tags. I have no desire to do anything more complicated than that. I am using pickle as the file format, so I dont need any help here on how to save it or in what format. Just need a way to pull/push the data so that the user loses nothing that he/she sees on screen. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只是想将文本复制到另一个文本缓冲区而需要保存标签,则可以使用 gtk.TextBuffer.insert_range()。
如果您需要将带有标签的文本保存为其他程序可读的另一种格式,我曾经编写过一个带有 GTK 文本缓冲区序列化器和 RTF 的库。但它没有任何 Python 绑定。但无论如何,代码都是如何使用序列化器工具的一个很好的示例。链接:Osxcart
If you need to save the tags because you just want to copy the text into another text buffer, you can use
gtk.TextBuffer.insert_range()
.If you need to save the text with tags into another format readable by other programs, I once wrote a library with a GTK text buffer serializer to and from RTF. It doesn't have any Python bindings though. But in any case the code is a good example of how to use the serializer facility. Link: Osxcart
我没有使用过 GtkTextBuffer 的序列化。阅读您链接的文档,我建议通过调用尝试默认序列化器,
这为您提供了 GTK+ 的内置专有序列化器。这里的专有意味着它不会序列化为某种众所周知的格式;但如果您需要的只是能够保存文本缓冲区的内容并将其加载回来,那么这应该没问题。
当然,如果您确实想了解 GTK+ 的工作原理,可以在 GTK+ 中找到源代码;不过,我建议不要尝试实现例如独立的反序列化器,因为 GTK+ 可能无法保证格式将保持原样。
I haven't worked with GtkTextBuffer's serialization. Reading the documentation you linked, I would suggest trying the default serializer, by calling
This gives you GTK+'s built-in proprietary serializer. Being proprietary here means that it doesn't serialize into some well-known format; but if all you need is the ability to save out the text buffer's contents and load them back, this should be fine.
Of course the source code is available inside GTK+ if you really want to figure out how it works; I would recommend against trying to implement e.g. a stand-alone de-serializer though, since there are probably no guarantees made by GTK+ that the format will remain as-is.