-2

This is not a copy of this question: How to highlight text in a tkinter Text widget. It's more of a continuation. From This Question, I got this code:

class CustomText(tk.Text):
    '''A text widget with a new method, highlight_pattern()

    example:

    text = CustomText()
    text.tag_configure("red", foreground="#ff0000")
    text.highlight_pattern("this should be red", "red")

    The highlight_pattern method is a simplified python
    version of the tcl code at http://wiki.tcl.tk/3246
    '''
    def __init__(self, *args, **kwargs):
        tk.Text.__init__(self, *args, **kwargs)

    def highlight_pattern(self, pattern, tag, start="1.0", end="end",
                          regexp=False):
        '''Apply the given tag to all text that matches the given pattern

        If 'regexp' is set to True, pattern will be treated as a regular
        expression according to Tcl's regular expression syntax.
        '''

        start = self.index(start)
        end = self.index(end)
        self.mark_set("matchStart", start)
        self.mark_set("matchEnd", start)
        self.mark_set("searchLimit", end)

        count = tk.IntVar()
        while True:
            index = self.search(pattern, "matchEnd","searchLimit",
                                count=count, regexp=regexp)
            if index == "": break
            if count.get() == 0: break # degenerate pattern which matches zero-length strings
            self.mark_set("matchStart", index)
            self.mark_set("matchEnd", "%s+%sc" % (index, count.get()))
            self.tag_add(tag, "matchStart", "matchEnd")

This code works very well tho I am running into an issue. I actually want to use this highlight feature in my python editor's color coding. The problem is that whenever I use this, I need it to color up words but it colors the letters up anywhere it finds it, even when its in other words. (For Example: When I try to highlight the word "on", the "on" in the word "one" will also color up) Can anyone please modify this code for me, so it only colors up words.

Ultra
  • 21
  • 2
  • You seem to know about regular expressions, so shouldn't it be quite easy to highlight `\bon\b`? – timgeb Jun 21 '20 at 04:43
  • Actually this isn't my code, its from this question: https://stackoverflow.com/questions/3781670/how-to-highlight-text-in-a-tkinter-text-widget. I'm not very good with regular expressions, so can you help me? (btw "\b" + word + "\b" didn't work) – Ultra Jun 21 '20 at 04:50
  • Wait actually, its not changing the color at all for some reason – Ultra Jun 21 '20 at 04:57
  • This is what I wrote: textArea.highlight_pattern(re.compile("\b" + Keyword + "\b"), "pyKeyword", regexp=True) Is there something wrong in it? – Ultra Jun 21 '20 at 05:12
  • Read up on what [`'\b'`](https://www.tcl.tk/man/tcl8.6/TclCmd/re_syntax.htm#M50) is for if `regxp=True`. – stovfl Jun 21 '20 at 08:23
  • Actually "\\y" worked, and yes I had regexp set to True – Ultra Jun 21 '20 at 10:57

1 Answers1

0

So I found this post: Search for whole words in the Text widget with the search method and this code seems to work perfectly:

textArea.highlight_pattern("\\y" + word + "\\y", "tag", regexp=True)
Ultra
  • 21
  • 2