TK canvas text bounding box question

Tkinter stumped a at b.c
Fri Sep 27 12:54:36 CEST 2002

Perhaps I am just stupid, but I have spend significant amounts of time
banging my head against this wall.

Using the canvas widget text item (so I can have per pixel control of text
placement), I can get the bounding box for the entire text item, but would
like (and cannot seem to get) the bounding box for a charater in the text

This seems like something that would have come up before (vis a vis Tkinter)
but a search of archives gave me nothing. I did find this page:, but if I am reading that code correctly, it seems
to be doing something like canvas.index(tagOrId, x, y) => characterIndex,
which won't work.

What am I missing (or just plain unaware of)? Am I missing something? Or is
there no way to do this short of the painful, ugly and slow brute-force
workaround that presents itself:

# x, y somewhere safely out of view
text1 = canvas.create_text(x,y, text="foo", anchor="nw", font=f)
bb_1 = canvas.bbox(text1)
text2 = canvas.create_text(x,y, text="foob", anchor="nw", font=f)
bb_2 = canvas.bbox(text2)

now subtracting bb_1 from bb_2 gives me a bounding box for 'b' in "foobar"
relative to x,y , and modulo the extra space on the end of a text item.

please tell me there is a better way to do this....

More information about the Python-list mailing list