[wxPython-users] TextCtrl widths

Robin Dunn robin at alldunn.com
Sat Nov 3 16:29:09 PDT 2007


Mark Erbaugh wrote:
> Is there an easy way to set the width of a TextCtrl to be wide enough
> for a certain number of characters rather than pixels. What I've been
> doing is setting the width by trial and error or eyeballing it. However,
> I imagine that if the user has a different font or screen resolution,
> the TextCtrl may be too small or too large.

It also depends on what text is present, since with a proportional font 
15 i's are a lot narrower than 15 W's.

> 
> I know I could use a Masked control, but I want to do this with a plain
> TextCtrl.

If you use a monospaced font then you can make a pretty good estimate of 
how wide the textctrl should be by making a wx.ClientDC, setting the 
same font and measuring a string with GetTextExtent.  Just add a few for 
the overhead needed for the widget's borders, margins, etc.  For 
proportional fonts you can use the average character width, but it won't 
always be exact because of the variable widths.


-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wxpython-users mailing list