如何以像素为单位获取字符串的宽度? [英] How to get the width of a string in pixels?

查看:24
本文介绍了如何以像素为单位获取字符串的宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 wxPython 的 HyperTreeList 并且我想将列宽设置为完全等于其中最大字符串的长度.

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

为此,我想将 python 字符串大小转换为像素.

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?

还有别的方法吗?

推荐答案

你必须做 某事 like(有关详细信息,请参阅 wxWidgets 的文档)

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

这篇关于如何以像素为单位获取字符串的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆