Вы можете использовать canvas.find_overlapping(x, y, x+1, y+1)
, чтобы найти границы вашего текста:
import tkinter as tk
def create_bg(item, bg='red'):
x0, y0, x1, y1 = canvas.bbox(item)
# start of last line
x = x0
y = y1 - 1
# find end x of last line
while x < x1 and item in canvas.find_overlapping(x, y, x + 1, y + 1):
x += 1
# find top y of last line
while y > y0 and item not in canvas.find_overlapping(x, y, x + 1, y + 1):
y -= 1
y += 1
vertices = [x0, y0, x1, y0, x1, y, x, y, x, y1, x0, y1]
bg = canvas.create_polygon(*vertices, fill=bg)
canvas.tag_lower(bg, item)
root = tk.Tk()
canvas = tk.Canvas(root)
canvas.pack()
item = canvas.create_text(20, 20, anchor='nw', width=150,
text='This is an example of wrapped text.')
item2 = canvas.create_text(20, 120, anchor='nw', font='Arial 20', width=150,
text='This is an example of wrapped text.')
create_bg(item)
create_bg(item2, 'cyan')
root.mainloop()
Скриншот