[wxPython-users] TextCtrl widths

Mark Erbaugh mark at microenh.com
Sat Nov 3 17:20:43 PDT 2007


On Sat, 2007-11-03 at 16:29 -0700, Robin Dunn wrote:
> 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.
> 

Thanks for the reply. I can see howproportional fonts create problems.
I was working with one UI library.  I notice that in Tkinter, Entry
widgets widths are specified in characters.  The documentation indicates
that for proportional fonts the average character width is used.

Mark





More information about the wxpython-users mailing list