python - 获取与 tkinter 文本小部件关联的所有标签?

标签 python tkinter tk-toolkit

我想知道如何一次获取 Text 小部件中的所有标签。所以假设这是我的内容:


(假设我每次输入 ("tag", text),我的意思是 text 有标签 tag。)

("keyword", print) ("string", "Hello world!")
("keyword", print) ("string", "You are"), ("support", raw_input())


现在假设这是每个标签的代码:

self.text.tag_config('keyword', foreground="orange")
self.text.tag_config('operator', foreground="blue")
self.text.tag_config('number', foreground="red")
self.text.tag_config('string', foreground="red")
self.text.tag_config('support', foreground="blue")
self.text.tag_config('comment', foreground="violet")
self.text.tag_config('error', background="red")
self.text.tag_config('warning', background="yellow")

现在,是否有任何函数/常量可以只获取我的字符串中当前的标签?它将报告 ['keyword', 'string', 'support']

(注意:大多数时候我只是想知道这个,因为我很好奇。如果没有,这不像是世界末日。:P)

注意:第二个代码块是用 Python 编写的,我希望在 Python 中得到答案。 :)

最佳答案

来自 my favorite tkinter reference

.tag_names(index=None)

If you pass an index argument, this method returns a sequence of all the tag names that are associated with the character after that index. If you pass no argument, you get a sequence of all the tag names defined in the text widget.

.tag_ranges(tagName) 列出使用特定标签的索引。

关于python - 获取与 tkinter 文本小部件关联的所有标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27991874/

相关文章:

Python3,理解 tkinter-mainloop() 或 QtPy-exec_() 中的 "GUI loop"

python - PyQtGraph 中的图形在缩放后未更新

Python 绘制中值

python - 在 OSX 上隐藏 Tkinter 应用程序的控制台

python - Tkinter - Canvas 滚动/滚动区域解释 - (非)限制区域

python - 如何修改数据库管理器中的重复表单?

python - 如何更改框架内标签的位置?

python - AttributeError: 图片没有属性 'open'

python - tkinter:应用程序终止后事件绑定(bind)仍然存在吗?

从 R 中的 TclTk GUI 返回变量