我想知道如何一次获取 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/