Search String in Text using Python-Tkinter Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report 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 - TtkinterMethod 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. Declare a variable s and get the string input of the user to be searched in the text( in this case the string is typed in 'edit' text box window) Initialize index value as 1. Initialize inner loop. Using .search() search for the desired string (in this case s) in the given text and update the current index value till the end of the text. Last Index value is the addition of the current index and length of the string. Add 'found' tag from index 1 to last index whenever a desired string is found in between. Change focus to find button Once find button is pressed. the string with 'found' tags, the string is highlighted in red. use .mainloop() for termination as any user will terminate the program Example 1: Python3 #Python Program to search string in text using Tkinter from tkinter import * #to create a window root = Tk() #root window is the parent window fram = Frame(root) #adding label to search box Label(fram,text='Text to find:').pack(side=LEFT) #adding of single line text box edit = Entry(fram) #positioning of text box edit.pack(side=LEFT, fill=BOTH, expand=1) #setting focus edit.focus_set() #adding of search button butt = Button(fram, text='Find') butt.pack(side=RIGHT) fram.pack(side=TOP) #text box in root window text = Text(root) #text input area at index 1 in text window text.insert('1.0','''Type your text here''') text.pack(side=BOTTOM) #function to search string in text def find(): #remove tag 'found' from index 1 to END text.tag_remove('found', '1.0', END) #returns to widget currently in focus s = edit.get() if s: idx = '1.0' while 1: #searches for desired string from index 1 idx = text.search(s, idx, nocase=1, stopindex=END) if not idx: break #last index sum of current index and #length of text lastidx = '%s+%dc' % (idx, len(s)) #overwrite 'Found' at idx text.tag_add('found', idx, lastidx) idx = lastidx #mark located string as red text.tag_config('found', foreground='red') edit.focus_set() butt.config(command=find) #mainloop function calls the endless loop of the window, #so the window will wait for any #user interaction till we close it 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 #Python Program to search string in text using Tkinter 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','''Type your 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() OUTPUT : <img src="https://media.geeksforgeeks.org/wp-content/uploads/20200327124532/tkinter21.png" alt="Search String in Text using Python-Tkinter"> Create Quiz Comment A akshay_sharma08 Follow 1 Improve A akshay_sharma08 Follow 1 Improve Article Tags : Python Python-tkinter Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like