I have a TextCtrl in my wxPython program and I’d like to set its width to exactly 3 characters. However, the only way to set its size manually accepts only numbers of pixels. Is there any way to specify characters instead of pixels?
I have a TextCtrl in my wxPython program and I’d like to set its
Share
Realize that most fonts are proportional, which means that each character may take a different width. WWW and lll are both 3 characters, but they will require vastly different sizes of text box. Some fonts, such as Courier, are designed to be fixed width and will not have this problem. Unfortunately you may not have any control over which font is selected in the text box.
If you still want to try this, the key is to get the width of a character in pixels, multiply it by the number of characters, then add some padding for the borders around the characters. You may find this to be a good starting point:
http://docs.wxwidgets.org/stable/wx_wxdc.html#wxdcgetpartialtextextents
or, as litb suggests:
http://docs.wxwidgets.org/2.4/wx_wxwindow.html#wxwindowgettextextent