Search string in Text : Text « GUI Tk « Python






Search string in Text

Search string in Text
 

from Tkinter import *

root = Tk()

fram = Frame(root)
Label(fram,text='Text to find:').pack(side=LEFT)
edit = Entry(fram)
edit.pack(side=LEFT, fill=BOTH, expand=1)
edit.focus_set()
butt = Button(fram, text='Find')
butt.pack(side=RIGHT)
fram.pack(side=TOP)

text = Text(root)
text.insert('1.0','''Search text 
                   Here
                  ''')
text.pack(side=BOTTOM)


def find():
    text.tag_remove('found', '1.0', END)
    s = edit.get()
    if s:
        idx = '1.0'
        while 1:
            idx = text.search(s, idx, nocase=1, stopindex=END)
            if not idx: break
            lastidx = '%s+%dc' % (idx, len(s))
            text.tag_add('found', idx, lastidx)
            idx = lastidx
        text.tag_config('found', foreground='red')
    edit.focus_set()
butt.config(command=find)
root.mainloop()

           
         
  








Related examples in the same category

1.Draw textDraw text
2.Draw text with fontDraw text with font
3.Scrolled TextScrolled Text
4.Change color for tagsChange color for tags
5.Change font for TextChange font for Text
6.Add double click action to a TextAdd double click action to a Text
7.Insert String to a TextInsert String to a Text
8.Text properties: expand and fillText properties: expand and fill
9.Text with ScrollBarText with ScrollBar
10.Show a file in a text widget