Apr-16-2021, 06:38 PM
Using \y works with your code for me on python 3.9.4
https://www.tcl.tk/man/tcl8.4/TclCmd/re_syntax.htm#M54 Wrote:\y matches only at the beginning or end of a word
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' case_insensitive = '(?i)' whole_word_left = '\y' whole_word_right = '\y' while True: idx = text_widget.search(f'{case_insensitive}{whole_word_left}{keyword}{whole_word_right}', pos, "end-1c", regexp=True) if not idx: break print(idx) pos = '{}+{}c'.format(idx, len(keyword)) text_widget.tag_add(tag, idx, pos) def main(): win = Tk() app = Application(win) win.geometry("800x600+1000+300") txt = Text(win, bg='cyan') txt.pack() txt.insert('end-1c', 'import\nimport time\nfrom time import sleep\nImpo signal\nImport sys\nimpo board\nimport busio\nImpo digitalio\nfrom digitalio import Direction, Pull\nfrom RPi import GPIO\n10from adafruit_mcp230xx.mcp23017 import MCP23017\n') txt.tag_config('passed', background='yellow') search(txt, 'impo', 'passed') win.mainloop() if __name__ == '__main__': main()
Output:4.0
6.0
8.0