Apr-16-2021, 03:31 PM
(This post was last modified: Apr-16-2021, 04:08 PM by Yoriz.
Edit Reason: Added prefix
)
My code below uses text_widget.search() with regexp=True but it's not finding my target word. Can someone tell me what I am missing?
I also have a non-regexp search which works and highlights the text widget found words. However, the regexp line doesn't find the words (idx returns "") and I'd like to use the regexp version because the non-regexp search() method does not have a whole word flag.
Thank you...
I also have a non-regexp search which works and highlights the text widget found words. However, the regexp line doesn't find the words (idx returns "") and I'd like to use the regexp version because the non-regexp search() method does not have a whole word flag.
Thank you...
from tkinter import *
class Application(object):
def __init__(self, main_win):
self.main_win = main_win
def search(text_widget, keyword, tag):
pos = '1.0'
while True:
#idx = text_widget.search(keyword, pos, "end-1c", nocase=0, exact=1) # nocase=1 ignores case
idx = text_widget.search('/' + keyword +'/g', pos, "end-1c", regexp=True)
if not idx:
break
pos = '{}+{}c'.format(idx, len(keyword))
text_widget.tag_add(tag, idx, pos)
def main():
win = Tk()
app = Application(win)
txt = Text(win, bg='cyan')
txt.pack()
txt.insert('end-1c', 'import\n' \
'Import\n'\
'Impo\n'\
'impo\n')
txt.tag_config('passed', background='yellow')
search(txt, 'impo', 'passed')
win.mainloop()
if __name__ == '__main__':
main()
