如何使用格式保存 Tkinter 文本小部件的内容 [英] How to save contents of a Tkinter text widget with formatting

查看:32
本文介绍了如何使用格式保存 Tkinter 文本小部件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 python 中使用 Tkinter 在文本窗口中显示输出.我发现使用 'get' 函数可以从这个窗口中检索文本内容.但是我有用不同背景颜色标记的文本部分.是否可以将内容与这些颜色一起复制到文件中,比如 html 或 doc?

I am using Tkinter in python to display the output in a text window. I found that with 'get' function I can retrieve the text content from this window. But I have text portions marked with different background colours. Is it possible to copy the content as such along with these colours to a file, say html or doc?

推荐答案

不支持您想要的.您可以调用 .dump() 方法,该方法将返回包括文本和标签在内的信息.但是,这些数据不是标准格式,也不支持重新加载数据.可以编写软件将其重新加载,但您必须自己完成所有工作.

There is no support for what you want. You can call the .dump() method which will return information including both the text and the tags. However, this data is not in a standard format and there's no support for loading the data back in. It's possible to write software to load it back in, but you have to do all the work yourself.

对返回内容的最佳描述来自 tcl/tk man页.它部分说明:

The best description of what is returned is from the tcl/tk man pages. It states in part:

pathName dump ?switches?索引1?索引2?

pathName dump ?switches? index1 ?index2?

返回从 index1 到但不包括 index2 的文本小部件的内容,包括有关标记、标签和嵌入​​窗口的文本和信息.如果未指定 index2,则默认为 index1 后一个字符.信息以下列格式返回:

Return the contents of the text widget from index1 up to, but not including index2, including the text and information about marks, tags, and embedded windows. If index2 is not specified, then it defaults to one character past index1. The information is returned in the following format:

key1 value1 index1 key2 value2 index2 ...

key1 value1 index1 key2 value2 index2 ...

可能的键值为 text、mark、tagon、tagoff、image 和 window.对应的值为文本、标记名称、标签名称、图像名称或窗口名称.索引信息是文本、标记、标签过渡、图像或窗口的开始索引.

The possible key values are text, mark, tagon, tagoff, image, and window. The corresponding value is the text, mark name, tag name, image name, or window name. The index information is the index of the start of the text, mark, tag transition, image or window.

注意该文档适用于 tcl 语言.在 tkinter 中,您将调用该方法为 widget.dump(...),它返回一个元组.

N.B. the documentation is for the tcl language. In tkinter you would call the method as widget.dump(...), and it returns a tuple.

这篇关于如何使用格式保存 Tkinter 文本小部件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆