Here is a snippet of code that will allow you to search for any regular expression in the text widget by using the tcl character indexing syntax:
import tkinter
import re
root = tkinter.Tk()
text = tkinter.Text(root)
text.pack()
def findall(pattern, start="1.0", end="end"):
start = text.index(start)
end = text.index(end)
string = text.get(start, end)
indices = []
if string:
last_match_end = "1.0"
matches = re.finditer(pattern, string)
for match in matches:
match_start = text.index("%s+%dc" % (start, match.start()))
match_end = text.index("%s+%dc" % (start, match.end()))
indices.append((match_start, match_end))
print(indices)
root.after(200, findall, pattern)
root.after(200, findall, r"w+")
However, if you are dependent on the tkinter.Text.search function, I believe the idlelib EditorWindow
class uses it to do its syntax highlighting.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…