'Highlighting the middle letter in the supplied text

I need to be able to highlight a letter in a different color in the text supplied to entry_sb than in self.canvas.text. As a result, the text should run, and the letter that will be emphasized in the word should be highlighted.

class Application(object):

     def __init__(self):
        root = tk.Tk()
        self.canvas = tk.Canvas(root,bg='black')
        self.canvas.pack()
        self.canvas_text = self.canvas.create_text(180, 130, text='',anchor=tk.NW,fill="white",font=('Arial',18))


        self.entry = tk.Entry(root)
        self.entry.bind("<Return>", self.entry_cb)
        self.entry.pack()
        self.entry.focus()
        root.mainloop()
     def animate_text(self, text, delta):
        delay = 0
        for i in range(len(text)+1):
            update_text = lambda s=text[i - 1:-1]: self.canvas.itemconfigure(self.canvas_text, text=s)
            self.canvas.after(delay, update_text)
            delay += delta


     def entry_cb(self, event):
       # s = self.entry.get().split()
        s = 'London is the capital of Great Britain'.split()
        s += [s[-1]]
        self.animate_text(s, skorost)

skorost = 400

app = Application()


Solution 1:[1]

You should use a tkinter.Text widget instead.

Procedure

  • Configure a tag named highlight, which is to be added for the text.
  • Create a highlight function which searches and highlights the words left
  • Call the highlight function everytime text on canvas is edited

Fix

import tkinter as tk

class Application(object):
    def __init__(self):
        root = tk.Tk()
        self.canvas = tk.Canvas(root,bg='black')
        self.canvas.pack()
        self.canvas_text = self.canvas.create_text(180, 130, text='',anchor=tk.NW,fill="white",font=('Arial',18))

        self.entry = tk.Text(root, width=30, height=1)
        self.entry.bind("<Return>", self.entry_cb)
        self.entry.pack()
        self.entry.focus()

        self.entry.tag_config('highlight', background='yellow')

        root.mainloop()

    def highlight(self, text):
        # clean all tags
        self.entry.tag_remove('highlight', '1.0', tk.END)

        count = tk.IntVar()
        index = self.entry.search(text, '1.0', tk.END, count=count)
        if index == "": 
            return
        self.entry.mark_set("matchStart", index)
        self.entry.mark_set("matchEnd", "%s+%sc" % (index, count.get()))
        self.entry.tag_add("highlight", "matchStart", "matchEnd")
    
    def animate_text(self, text, delta):
        delay = 0
        for i in range(len(text)+1):
            def foo(s):
                self.canvas.itemconfigure(self.canvas_text, text=s)
                self.highlight(s)
            
            update_text = lambda s=text[i - 1:-1]: foo(s)
            self.canvas.after(delay, update_text)

            delay += delta

    def entry_cb(self, event):
        s = self.entry.get('1.0', tk.END).split()
        s += [s[-1]]
        self.animate_text(s, skorost)
        return 'break'

skorost = 400

app = Application()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1