The Right Way To Find The Size Of Text In Wxpython
I have an application I am developing in wxPython. Part of the application creates a large number of TextCtrls in a grid in order to enter four-letter codes for each day of the we
Solution 1:
Create a wx.Font instance with the face, size, etc.; create a wx.DC; then call dc.GetTextExtent("a text string") on that to get the width and height needed to display that string. Set your row height and column width in the grid accordingly.
Something like:
font = wx.Font(...)
dc = wx.DC()
dc.SetFont(font)
w,h = dc.GetTextExtent("test string")
Solution 2:
Not to take away from Paul McNett's answer, but for those not-so-familiar with wxPython here's a simple complete example:
import wx
app = wx.PySimpleApp()
font = wx.Font(pointSize = 10, family = wx.DEFAULT,
style = wx.NORMAL, weight = wx.NORMAL,
faceName = 'Consolas')
dc = wx.ScreenDC()
dc.SetFont(font)
text = 'text to test'# (width, height) in pixelsprint'%r: %s' % (text, dc.GetTextExtent(text))
Outputs: 'text to test': (84, 15)
Post a Comment for "The Right Way To Find The Size Of Text In Wxpython"