Search code examples
pythontkinter

Tkinter. More than one tag in textbox


I want to tag some words and highlight, but more than one tag is not created


    def open_txt_md_file(self):
        coincidences = []
        with open(self.path, "r") as f:
            n = 0

            for i in f:
                index = 0
                coincidences.append([])
                for x in i:
                    if index >= len(i): break

                    result = i[index:].find(self.find_text)
                    if result != -1:
                        coincidences[n].append(index + result)
                        index += result + 1

                if len(coincidences[n]) > 0:
                    self.text_1.insert(END, i)
                    for x in coincidences[n]:
                        start = "%s.%s" % (n+1, x)
                        end = "%s.%s" % (n+1, len(self.find_text)+1)
                       
                        self.text_1.tag_add("find %s" % start, start, end)
                        self.text_1.tag_config("find %s" % start, background="#00D8EB")
            
                n += 1 

I tried creating tags with different names. I checked with the tag_names function and they were created, but not displayed


Solution

  • Firstly, end is calculated wrong, it should be:

    end = "%s.%s" % (n+1, x+len(self.find_text))
    

    Secondly, the line self.text_1.insert(tk.END, i) should be called before the if len(coincidences[n]) > 0: block.

    Below is an simplified example:

    import tkinter as tk
    
    class App(tk.Tk):
        def __init__(self):
            super().__init__()
    
            self.find_text = "self"
            self.path = __file__
    
            self.text_1 = tk.Text(self, width=80, height=20)
            self.text_1.pack()
    
            self.open_txt_md_file()
    
        def open_txt_md_file(self):
            coincidences = []
            with open(self.path, "r") as f:
                n = 0
    
                for i in f:
                    index = 0
                    coincidences.append([])
                    for x in i:
                        if index >= len(i): break
    
                        result = i[index:].find(self.find_text)
                        if result != -1:
                            coincidences[n].append(index + result)
                            index += result + 1
    
                    self.text_1.insert(tk.END, i)
                    if len(coincidences[n]) > 0:
                        for x in coincidences[n]:
                            start = "%s.%s" % (n+1, x)
                            end = "%s.%s" % (n+1, x+len(self.find_text))
                            print(f"{start=} {end=}")
    
                            self.text_1.tag_add("find %s" % start, start, end)
                            self.text_1.tag_config("find %s" % start, background="#00D8EB")
    
                    n += 1
    
    app = App()
    app.mainloop()
    

    Result:

    enter image description here