Searching text

When we need to find a specific piece of text within a Text widget, there is a method called search which will allow us to do this easily.

The search method can take quite a lot of arguments:

  • pattern: The pattern to match. This can be either an exact match or a regular expression.
  • index: Where to begin the search from.
  • stopindex: Where to stop ending a search. If this is not specified, the search will loop.
  • forwards: Whether to search from the top to the bottom (this is the default).
  • backwards: Whether to search from bottom to top.
  • exact: Exact match instead of a regular expression (this is the default).
  • regexp: Indicates that the pattern supplied is a regular expression.
  • nocase: Whether to ignore case.
  • count: A variable which will be updated with the length of the match.

The only mandatory arguments are the search pattern and the starting index.

The Search method will return the index of the beginning of the match and, if supplied, a variable for the count argument; the length of the match will be stored inside it.

For example, if your Text widget had one line containing I like Python Programming and you passed the string Python to the pattern argument, the return value of the function would be 1.8, since the word Python begins at the eighth character of the line. Our count variable, if supplied, would contain the number 6.

We can use these two pieces of information to construct the starting and ending index which we will need to tag this match. The easiest way to do this is using the special string of +nc along with the count variable—append this to the index which is returned by the method.

In our example, the tagging indexes would be 1.8 for the start and 1.8+6c for the end.

Since this method returns after the first match is found, we will often need to run this in some sort of loop in order to find all pattern matches.

Let's get our heads around this by adding some code to our demo application which will detect and tag the word python throughout the Text widget.

Add this function alongside the others:

def tag_python(event=None):
text.tag_configure('python', foreground="green")
start = 1.0
idx = text.search('python', start, stopindex=tk.END)
while idx:
tag_begin = idx
tag_end = f"{idx}+6c"
text.tag_add('python', tag_begin, tag_end)

start = tag_end
idx = text.search('python', start, stopindex=tk.END)

return "break"

We will be using a tag called python to turn instances of the word green. We begin by configuring that tag and setting a starting variable to 1.0 (the beginning of the Text widget).

We then use the search method to find the word python. We use the start variable as the beginning index and the END constant to specify that we will finish at the end of the area.

The result of this method is stored in a variable called idx. This will contain the first occurrence of the word. Now, in order to find any further matches, we need to begin a loop. We use a while loop, which will exit once we no longer have a match index. This also ensures that the code inside will only run if we have at least one match.

Inside this loop, we create the tag range as mentioned before and add the Python tag to this range. We then update the start index to the calculated end index to ensure we begin searching from right after the match, and call the search method again.

This loop will continue to run until we reach the end of the text input area.

Bind this to a keyboard shortcut and run the application:

text.bind('<Control-p>', tag_python)

Write some text in the application which contains the word Python a few times. Pressing this keyboard combination will color all of them green.

This is the technique we will be using in order to implement syntax highlighting in our text editor. Instead of binding this to a keyboard combination, we will need our highlighting method to run as the user types, so we will be binding it to either <KeyPress> or <KeyRelease>.

Now that we have all of the tools and knowledge we need, we can finally begin with the syntax highlighting in our main text editor application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset