Implementing the Find Text feature
Next, let's code the Find Text
feature (refer to 2.05.py
in the code bundle). The following screenshot shows an example of the Find Text
feature:
Here's a quick summary of the desired functionality. When a user clicks on the Find menu item, a new Toplevel window opens up. The user enters a search keyword and specifies whether the search needs to be case-sensitive. When the user clicks on the Find All button, all matches are highlighted.
To search through the document, we will rely on the text_widget.search()
method. The search
method takes in the following arguments:
search(pattern, startindex, stopindex=None, forwards=None, backwards=None, exact=None, regexp=None, nocase=None, count=None)
For the editor, define a function called find_text
and attach it as a callback to the Find menu (refer to 2.05.py
in the code bundle):
edit_menu.add_command(label='Find',underline= 0, accelerator='Ctrl+F', command=find_text)
Also, bind it to the Ctrl + F shortcut, as follows...