Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain Tkinter text search method

I don't quite understand how text.search method works. For example there is a sentence: Today a red car appeared in the park. I need to find a red car sequence and highlight it. It is found but here is how my highlighting looks like:

enter image description here

enter image description here

I am using self.text.search(word, start, stopindex=END) on the sentence. And it looks like search method works exactly like python's regexp search. Adding exact=True didn't change anything since it is default behavior which is why I don't understand what exact=True actually means. How to make a red car highlighted correctly?

like image 619
minerals Avatar asked Oct 19 '13 10:10

minerals


People also ask

What is a tkinter method?

Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create the GUI applications. Creating a GUI using tkinter is an easy task.

What is TextBox in tkinter?

The Text widget is used to show the text data on the Python application. However, Tkinter provides us the Entry widget which is used to implement the single line text box. The Text widget is used to display the multi-line formatted text with various styles and attributes.

What does text variable do in tkinter?

In the case of textvariable , which is mostly used with Entry and Label widgets, it is a variable that will be displayed as text. When the variable changes, the text of the widget changes as well.

What is tkinter in Python with example?

Tkinter is the most commonly used library for developing GUI (Graphical User Interface) in Python. It is a standard Python interface to the Tk GUI toolkit shipped with Python.


2 Answers

The search method returns the index of the first match at or after the starting index, and optionally the number of characters that matched. You are responsible for highlighting what it found by using this information.

For example, consider this search:

countVar = tk.StringVar()
pos = text.search("a red car", "1.0", stopindex="end", count=countVar)

If a match is found, pos will contain the index of the first character of the match and countVar will contain the number of characters that matched. You can use this information to highlight the match by using an index of the form "index + N chars" or the shorthand "index + Nc". For example, if pos was 2.6 and count was 9, the index of the last character of the match would be 2.6+9c

With that, and assuming you've already configured a tag named "search" (eg: text.tag_configure("search", background="green")), you can add this tag to the start and end of the match like this:

text.tag_add("search", pos, "%s + %sc" (pos, countVar.get()))

To highlight all matches, just put the search command in a loop, and adjust the starting position to be one character past the end of the previous match.

like image 66
Bryan Oakley Avatar answered Oct 28 '22 20:10

Bryan Oakley


It may be a problem of the indexes.

In a program of mine, i have to search the start index and calculate the end index

my method for example, it works fine:

def highlight(self):
    start = 1.0
    pos = self.area_example.search(self.item.name, start, stopindex=END)
    while pos:
        length = len(self.item.name)
        row, col = pos.split('.')
        end = int(col) + length
        end = row + '.' + str(end)
        self.area_example.tag_add('highlight', pos, end)
        start = end
        pos = self.area_example.search(self.item.name, start, stopindex=END)
    self.area_example.tag_config('highlight', background='white', foreground='red')
like image 20
thinker3 Avatar answered Oct 28 '22 20:10

thinker3