I created a search function to parse text in a scrolled text box and it will find the first entry, and I want to add a function that will look for the next word that matches on a button. I found a post that had something similar and I tried replicating that in my code but I think it is effected by the trace function I use to parse the scrolled text box as letters are typed into the entry box. I assume I have to use tag_nextrange in the next_match() function. Can anyone lead me in the right direction?
Thanks in advance!
Here is what my code looks like:
def find_text(event):
def get_text(var, indx, mode):
word = my_var.get()
start = '1.0'
results_text.tag_remove('found', start, END)
if word:
start = results_text.search(word, start, nocase=1, stopindex=END)
last = '%s+%dc' % (start, len(word))
results_text.tag_add('found', start, last)
results_text.tag_config('found', background='yellow')
def next_match():
#This is what I tied but doesn't work.
while 'found' in results_text.tag_names('found'):
n_match = results_text.tag_nextrange('found', END)
if n_match:
results_text.mark_set('found', n_match[0])
results_text.see('found')
my_var = StringVar()
my_var.trace_add('write', get_text)
findstr = Toplevel()
findstr.wm_title("Find")
findstr.wm_iconbitmap("cs_icon.ico")
findstr.resizable(width = False, height = False)
find_label = Label(findstr, text="Enter your search query:", font=("Helvetica", 12))
find_label.grid(row=0, column=0,columnspan=3, sticky='nsew')
find_entry = Entry(findstr, bd=4, textvariable=my_var, width=100)
find_entry.grid(row=1, column=0, columnspan=3,padx=10, sticky='nsew')
find_entry.focus()
next_btn = Button(findstr, text="Next", width=20, command=next_match)
next_btn.grid(row=2, column=2, padx=10, pady=5, sticky='e')
#Binds Ctrl + f key to find_text function.
root.bind("<Control-f>", find_text)