Searching for a list of words within a Tkinter text widgit in Python 2.7 -


i've been trying button check on tkinter gui search entered text text widget specific word , make appear red, i've managed using following code:

list_of_words = ["foo", "bar`enter code here`"] def check(): global counter text.tag_remove('found', '1.0', end) idx = '1.0' x = 0 while true:     idx = text.search(list_of_words[x], idx, nocase=1, stopindex=end)     if not idx: break      lastidx = '%s+%dc' % (idx, len(list_of_words[x]))     text.tag_add('found', idx, lastidx)     idx = lastidx     text.tag_config('found', foreground='red')     counter += 1     print counter 

however need able search input words on list_of_words list , display them red. there way of doing this?

your code not increment x so, if first word present, while loop never terminate. does, however, increment global variable counter no apparent reason.

why not iterate on list of target words loop? inner while loop search text widget instances of each word, tagging them highlighting. termination condition while loop current word not found in widget. then, after words have been tagged, set colour.

def check():     text.tag_remove('found', '1.0', end)      word in list_of_words:         idx = '1.0'         while idx:             idx = text.search(word, idx, nocase=1, stopindex=end)             if idx:                 lastidx = '%s+%dc' % (idx, len(word))                 text.tag_add('found', idx, lastidx)                 idx = lastidx      text.tag_config('found', foreground='red') 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -