Need urgent solution......................
Mickel Grönroos
mickel at csc.fi
Thu Jun 12 01:16:13 EDT 2003
On 11 Jun 2003, Suresh Kumar wrote:
> Hi,
>
> 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.
I reckon this is what you want:
-------------------------------------------------
import Tkinter
root = Tkinter.Tk()
canvas = Tkinter.Canvas(root)
canvas.pack()
text = canvas.create_text(100,100,text="Hello World!")
x0,y0,x1,y1 = canvas.bbox(text) ## The coords of the bounding box
rect = canvas.create_rectangle(x0,y0,x1,y1) ## To demonstrate
textwidth = x1-x0 ## The width of the text in pixels
print textwidth ## To demonstrate
root.mainloop()
-------------------------------------------------
Cheers,
/Mickel
--
Mickel Grönroos, application specialist, linguistics, Research support, CSC
PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
CSC is the Finnish IT center for science, www.csc.fi
More information about the Python-list
mailing list