thanks for the code
I will check it and try to understand it
I modify your code and add another data with thw same first sentence
I will check it and try to understand it
I modify your code and add another data with thw same first sentence
import tkinter as tk data = """'This is line 1.' 'This is line 2.' 'Yet another line.' 'And finally the last line.' """ data2 = """'This is line 1.' 'Yet another line.' """ root = tk.Tk() txtwidget = tk.Text(root) for line in data: txtwidget.insert(tk.END, line) txtwidget.pack(expand=1, fill=tk.BOTH) for line2 in data2: txtwidget.insert(tk.END, line2) txtwidget.pack(expand=1, fill=tk.BOTH) # adding a tag to a part of text specifying the indices def highlight_text(tag_name, lineno, start_char, end_char, bg_color=None, fg_color=None): txtwidget.tag_add(tag_name, f'{lineno}.{start_char}', f'{lineno}.{end_char}') txtwidget.tag_config(tag_name, background=bg_color, foreground=fg_color) # text.tag_add("start", "1.8", "1.13") # text.tag_config("start", background="black", foreground="yellow") highlight_text(tag_name='tag1', lineno=1, start_char=1, end_char=5, fg_color='red') highlight_text(lineno=1, start_char=9, end_char=15, bg_color="black", fg_color='yellow', tag_name='zingo') root.mainloop()my second data is not colored like the first data