Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the width of a string in pixels?

I am using wxPython's HyperTreeList and I want to set the column width exactly equal to length of the largest string in it.

To accomplish that, I'd like to to convert a python string size into pixels.

For Example: If we have a string like

str = "python"
len(str) = 6

How could I convert the above string length/size into pixels?

Is there another way?

like image 920
MA1 Avatar asked Mar 16 '10 14:03

MA1


2 Answers

You'll have to do something like (see the documentation of wxWidgets for more info)

f = window.GetFont()
dc = wx.WindowDC(window)
dc.SetFont(f)
width, height = dc.GetTextExtent("Text to measure")
like image 176
Esteban Küber Avatar answered Oct 03 '22 01:10

Esteban Küber


It depends on how you are printing the text.

You may be interested by PIL ImageDraw which has a textsize method. See http://effbot.org/imagingbook/imagedraw.htm

Update: This was answering the original question. It may looks a little off-topic after question updates.

like image 38
luc Avatar answered Oct 03 '22 02:10

luc