finding size of text on Tkinter.Canvas, was: Re: Need urgent solution

Abel Daniel abli at freemail.hu
Wed Jun 11 14:07:22 EDT 2003


Suresh  Kumar wrote:
>   I am using python and tkinter. My question is a simple one. How 
> to find number of pixels that a text occupies in canvas? In 
> otherwords how can i get the width of the text whose width is not 
> set explicitly? I have placed a text, say "Hello World",  in the 
> canvas using "create_text" and want to find number pixels that it 
> occupies.
> 
> The text is created as follows.
>          self.canvas.create_text(100,100, text="Hello World"))
>    The text starts at 100,100 and how to find the ending points 
> of the text?
>    I need the solution urgently. Please give your comments as 
> soon as possible.
> 
> With Regards
> Suresh.

>>> import Tkinter
>>> c=Tkinter.Canvas()
>>> c.pack()
>>> text = c.create_text(100,100, text="Hello World")
>>> c.bbox(text)
(67, 93, 133, 107)
>>> 

Note that with the default setting, the _middle_ of the text will be at
the position you give. If you want to set the top-left corner of the
text at that given postion, you have to use the 'anchor' option:
text = c.create_text(100,100, text="Hello World", anchor=Tkinter.NW)

(As a reference text, i suggest downloading "Tkinter reference: a GUI
for Python" from http://www.nmt.edu/tcc/help/pubs/lang.html)

Abel Daniel





More information about the Python-list mailing list