Python 如何在tkinter文本框中获取正在搜索的单词的索引

Python 如何在tkinter文本框中获取正在搜索的单词的索引,python,tkinter,Python,Tkinter,我是python新手。我在用Tkinter制作ui。 我有一个文本框,用户可以在其中写入多行。 我需要在该行中搜索某些单词并突出显示 当前,当我在其中搜索所需单词并尝试使用“tag_configure”和“tag_add”着色时,我得到错误“bad index”。 在网上阅读某些页面后,我了解到“tag_add”中使用的起始索引和结束索引的格式为“row.column”[如果我在某个地方出错,请纠正我]。有谁能帮我直接从tkinter ui获取这种格式的索引以突出显示。它必须是浮点数-例如,文本

我是python新手。我在用Tkinter制作ui。 我有一个文本框,用户可以在其中写入多行。 我需要在该行中搜索某些单词并突出显示

当前,当我在其中搜索所需单词并尝试使用“tag_configure”和“tag_add”着色时,我得到错误“bad index”。
在网上阅读某些页面后,我了解到“tag_add”中使用的起始索引和结束索引的格式为“row.column”[如果我在某个地方出错,请纠正我]。有谁能帮我直接从tkinter ui获取这种格式的索引以突出显示。

它必须是浮点数-例如,文本中的第一个字符是
1.0
(而不是字符串
“1.0”


编辑:我弄错了。它可以是字符串-它必须是字符串,因为
1.1
1.10
是同一个浮点数(如上所述)-但我留下这个工作示例



为什么不向我们显示原始错误消息?你的代码呢?请看你的陈述“它必须是一个浮点数”是不正确的。它不是一个浮点数,它是一个“line.character”形式的字符串。它可能看起来像一个浮点数,但它不是。例如,浮点数1.1和1.10表示相同的数字,但它们在文本小部件中不表示相同的字符。您现在看到我的错误
1.1和1.10
-我一直使用浮点,但仅
1.0
来清除所有文本。谢谢大家。。在这篇文章的帮助下,我做到了这一点。。
from Tkinter import *

#------------------------------------

root = Tk()

#---

t = Text(root)
t.pack()

t.insert(0.0, 'Hello World of Tkinter. And World of Python.')

# create tag style
t.tag_config("red_tag", foreground="red", underline=1)

#---

word = 'World'

# word length use as offset to get end position for tag
offset = '+%dc' % len(word) # +5c (5 chars)

# search word from first char (1.0) to the end of text (END)
pos_start = t.search(word, '1.0', END)

# check if found the word
while pos_start:

    # create end position by adding (as string "+5c") number of chars in searched word 
    pos_end = pos_start + offset

    print pos_start, pos_end # 1.6 1.6+5c :for first `World`

    # add tag
    t.tag_add('red_tag', pos_start, pos_end)

    # search again from pos_end to the end of text (END)
    pos_start = t.search(word, pos_end, END)

#---

root.mainloop()

#------------------------------------