将 gtk.TextTags 保存到文件?
因此,我尝试在 PyGTK 中编写一个富文本编辑器,最初使用 Gourmet 的较旧的第三方脚本 InteractivePangoBuffer 来执行此操作。虽然它工作正常,但仍然存在很多错误,有时使用起来令人沮丧,所以我决定编写自己的文本标签。我已经让它们显示出来并且通常工作正常,但现在我一直试图弄清楚如何在保存时将它们导出到文件中。我看到其他人也遇到了与我相同的问题,尽管我还没有看到任何解决方案。我还没有遇到任何函数(内置或其他)接近于实际获取每段文本的开始和结束位置并应用了文本标签,以便我可以使用它。
我想出了一个理论上可行的想法,通过利用 gtk.TextBuffer.get_iter_at_offset()、gtk.TextIter.get_offset()、gtk.TextIter.begins_tag() 和 gtk.TextIter.ends_tag() 遍历文本为了检查每个字符以查看它是否开始或结束标签,如果是,则输入适当的代码。然而,这会非常低效且缓慢,尤其是在较大的文档上,所以我想知道是否有人有更好的解决方案?
So I am trying to write a rich text editor in PyGTK, and originally used the older, third party script InteractivePangoBuffer from Gourmet to do this. While it worked alright, there were still plenty of bugs with it which made it frustrating to use at times, so I decided to write my own utilizing text tags. I have got them displaying and generally working alright, but now I am stuck at trying to figure out how to export them to a file when saving. I've seen that others have had the same problem I've had, though I haven't seen any solutions. I haven't come across any function (built in or otherwise) which comes close to actually getting the starting and ending position of each piece of text with a texttag applied to it so I can use it.
I have come up with one idea which should theoretically work, by walking the text by utilizing gtk.TextBuffer.get_iter_at_offset(), gtk.TextIter.get_offset(), gtk.TextIter.begins_tag(), and gtk.TextIter.ends_tag() in order to check each and every character to see if it begins or ends a tag and, if so, put the appropriate code. This would be horribly inefficient and slow, especially on larger documents, however, so I am wondering if anyone has any better solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您也许可以使用 gtk.TextIter.forward_to_tag_toggle() 。即循环遍历您拥有的所有标签,并为每个标签扫描缓冲区以查找其切换的位置。
You can probably use
gtk.TextIter.forward_to_tag_toggle()
. I.e. loop over all tags you have and for each tags scan the buffer for the position where it is toggled.