需要有关 GtkTextBuffer(GtkTextView)序列化/反序列化的示例/帮助

发布于 2024-08-14 06:24:44 字数 562 浏览 4 评论 0原文

我正在尝试将用户的粗体/斜体/字体/等标签保存在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话难免假 2024-08-21 06:24:44

如果您只是想将文本复制到另一个文本缓冲区而需要保存标签,则可以使用 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

筑梦 2024-08-21 06:24:44

我没有使用过 GtkTextBuffer 的序列化。阅读您链接的文档,我建议通过调用尝试默认序列化器,

textbuffer.register_serialize_tagset()

这为您提供了 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

textbuffer.register_serialize_tagset()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文