-1

i need to find and highlight with color OR bold specific words in string.

i thought on doing like this

word to put in red=is:

var="my string is that should is appear with the word flame is in red is"

varf=var.replace(" is ","\033[1;32;40m  is ")

but this dont work on tkinter ScrolledText, just in terminal :s

is there a way to do this in a tkinter ScrolledText widget?

Word will appear in diferent places in string so its hard to do the config.tag because i have to specify the interval of characters in the string to color.

my code

def checkval(e, spec0, spec1, spec2, spec3, spec4, spec5):
        e.widget.insert("1.0", PLACEHOLDER if e.widget.get("1.0", "end-1c") == "" else "")
        reqlidst=(spec0, spec1, spec2, spec3, spec4, spec5)
        textlabl=""
        for x in reqlidst:
   
            if len(x)>1:
                #print("selected text: '%s'" % e.widget.get(SEL_FIRST, SEL_LAST))
                if x.upper() in e.widget.get("1.0", "end-1c").upper():
                    if len(textlabl)>1:
                        textlabl = textlabl + "- " + x.replace(" ", "").upper() + "  " + html.unescape('✔')+"\n\n"
                    else:
                        textlabl = "- " + x.replace(" ", "").upper() + "  " + html.unescape('✔')+"\n\n"
                else:
                    if len(textlabl) > 1:
                        textlabl =textlabl + "- " + x.replace(" ", "").lower() + "  " + html.unescape('✘')+"\n\n"
                    else:
                        textlabl = "- " + x.replace(" ", "").lower() + "  " + html.unescape('✘')+"\n\n"
        e.widget.my_var_expected.set(textlabl)

reqlidst are the word to search for and show in red

How to change the color of certain words in the tkinter text widget? doesnt answer my question :/

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
mclogan
  • 35
  • 5
  • You write that `How to change the color of certain words in the tkinter text widget?` - why not? Did you try it? It explains how to change the colors of text in a text widget. – Bryan Oakley Oct 07 '21 at 15:34
  • 1
    Does this answer help? https://stackoverflow.com/a/3781773/7432 – Bryan Oakley Oct 07 '21 at 15:40
  • the thing is that i have more than one word to search and the text and words are diferent, i cant get it working, how am i supose to get the SEL_FIRST, SEL_LAST? – mclogan Oct 07 '21 at 17:13
  • You don't have to use `SEL_FIRST` and `SEL_LAST`. You can use any index supported by the text widget. The answer I linked to is an example of using something other than those indexes. – Bryan Oakley Oct 07 '21 at 17:48
  • @BryanOakley thanks, i have to admit that code is a little confusing for me but ill try. One thing, im using ScrolledText widget will it work with it? – mclogan Oct 08 '21 at 11:03

1 Answers1

0

There is a tag method in the Tkinter text. This allows you to mark certain parts. I have prepared a small example for you, where the Text-Widget dynamically recognizes the words Hello and Jonny.

In principle, you only really need to be familiar with the indexes.

from tkinter import Text,  Button, Tk

root = Tk()


def output(event):

    length = len("Hello")
    val = len(txt.get("1.0", "end-1c"))
    for x in range(len(txt.get("1.0", "end-1c"))):
        if "Hello" in txt.get(f"1.{x}", "end-1c") or "Jonny" in txt.get(f"1.{x}", "end-1c") :
            val = x
    vals = str(val + length)
    txt.tag_add("Hello", f"1.{str(val)}", f"1.{vals}")
    txt.tag_config("Hello", background="red", foreground="white")

txt = Text(root)
txt.grid(columnspan=3)

root.bind("<Key>", output)

root.mainloop()

enter image description here

Please note that I did not write a whole program for each new line.

For example, to make it more dynamic, you could change the indexes. So you can implement that for each line.

My example is for the first line only.

greeting

BanAnn
  • 159
  • 1
  • 7