How to get the width of a string in pixels?

你。 提交于 2019-12-30 04:28:06

问题


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?


回答1:


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")



回答2:


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.



来源:https://stackoverflow.com/questions/2455255/how-to-get-the-width-of-a-string-in-pixels

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!