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.
- 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
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.
- Python | Splitting Text and Number in string
- Convert Text and Text File to PDF using Python
- Python: Convert Speech to text and text to Speech
- PyQt5 - How to set text to progress bar ?
- Processing text using NLP | Basics
- PyQt5 QSpinBox – Getting whole text
- Text detection using Python
- Text Analysis in Python 3
- wxPython - Add text in window
- Text Preprocessing in Python | Set 2
- Python | Text Summarizer
- PyQt5 QCommandLinkButton - Getting Help Text
- NLP | Categorized Text Corpus
- pgmagick - Add text in images
- UwU text convertor in Python
- PyQt5 QSpinBox - Getting right bearing value of the text
- Speech To Text using IBM Watson Studio
- Text Detection and Extraction using OpenCV and OCR
- Wand text() function in Python
- Extract text from PDF File using Python