如何在 tkinter canvas 中更改文本对象中间单个单词的颜色?

How to change the color of a single word in the middle of a text object in tkinter canvas?

我在 canvas 中有多行文本,我想更改单个单词的颜色,但我无法使用 insert() 更改它,有没有办法它?另外,如何在多行的 create_text() 对象上找到最后一个单词的位置?

from tkinter import font
import tkinter as tk

root = tk.Tk()
c = tk.Canvas(root)
c.pack(expand=1, fill=tk.BOTH)

fn = "Helvetica"
fs = 24
font = font.Font(family=fn, size=fs)
    
word1 = "I would like the last word of this phrase to be another color, maybe "
word2 = "red"
word3 = "... some other text that should be black again"

t1 = c.create_text(50,50,text=word1, anchor='nw', font=font, width=600)

#I would like this next word to be another color (red, green...)
c.insert(t1, "end", word2)

#then I would like it to be black again
c.insert(t1, "end", word3)

root.geometry('800x500+200+200')
root.mainloop()

测试此代码;

import tkinter as tk
from tkinter import font

root = tk.Tk()
c = tk.Canvas(root)
c.pack(expand=1, fill=tk.BOTH)

fn = "Helvetica"
fs = 24
font = font.Font(family=fn, size=fs)

sentence = '''
I would like the last word of this phrase to be another color, maybe red ... some other text that should be black again
'''

def change_color(sentence, color, start_p, end_p):
    sentence = sentence.rstrip()
    t1 = c.create_text(50, 50, text=sentence, anchor='nw', font=font, width=600)
    t2 = c.create_text(50, 50, text=sentence[:end_p], anchor='nw', font=font, width=600, fill=color)
    t3 = c.create_text(50, 50, text=sentence[:start_p], anchor='nw', font=font, width=600)


change_color(sentence=sentence, color="red", start_p=70, end_p=73)
root.geometry('800x500+200+200')
root.mainloop()

start_p是开始位置,end_p是要改变颜色的子串的结束位置。