Search String in Text using PythonTkinter (original) (raw)

Search String in Text using Python-Tkinter

Last Updated : 26 Oct, 2021

Tkinter is the standard GUI library for Python. It provides a powerful object-oriented interface to the Tk GUI toolkit. In this article, we’ll see how to search for a specific string in a given text window using Tkinter.
NOTE : For more detailed information on Tkinter, refer to Python GUI – Ttkinter
Method to create user-defined function to search(def find)
An inner loop will search the text widget for all instances of each word, tagging them for highlighting. The termination condition for the loop is that the current word was not found in the widget. Then, after all, words have been tagged, set their color.

Example 1:

Python3

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' , )

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()

OUTPUT :

<img src="https://media.geeksforgeeks.org/wp-content/uploads/20200327124102/tkinter2.png" alt="Search String in Text using Python-Tkinter
“>

The larger text box is for the text input and the smaller text box is for the string input that needs to be found in the given text and once found, it is marked in red.
Example 2:

Python3

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' , )

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()

OUTPUT :

<img src="https://media.geeksforgeeks.org/wp-content/uploads/20200327124532/tkinter21.png" alt="Search String in Text using Python-Tkinter
“>

Similar Reads

Introduction






Widgets













































Geometry Management









Binding Functions




Working with Images in Tkinter






Tkinter Advance




















Applications and Projects