How do I create a font

Fonts are specified as instances of the class wx.Font. You have access to any font that has been installed and is accessible to the underlying system. To create a font instance, use the following constructor:

wx.Font(pointSize, family, style, weight, underline=False, faceName="", encoding=wx.FONTENCODING_DEFAULT)

The pointSize is the font's integer size in points. The family is used to quickly specify a font without having to know the actual name of the font. The exact font chosen depends on the system and specific fonts available. A sample of available font families are displayed in table 7.7. The exact fonts you get will depend on your system.

Table 7.7 Sample of the existing font families




A formal, old-English style font


The system default font


A monospace (fixed-pitch) font

Table 7.7 Sample of the existing font families (continued)




A serif font, generally something like Times New Roman


A handwriting er cursive font


A sans-serif font, generally something like Helvetica or Arial

The style parameter indicates the italicized nature of the font, and is either wx.normal, wx.slant, or wx.italic. Similarly, the weight parameter indicates the boldness of the font, and is either wx.normal, wx.light, or wx.bold. The constants here behave as expected based on their name. The underline parameter works only on Windows systems, and if set to True causes the font to be underlined. Use the faceName argument to specify the system name of the font you want to display.

The encoding parameter allows you to select one of several encodings, which are mappings between internal characters and font display characters. Encodings are not Unicode encodings, just different 8-bit encodings used by wxPython. For most usage, you can use the default encoding.

To retrieve a list of available fonts on the system, and make them available to the user, use the special class wx.FontEnumerator as in the following:

e = wx.FontEnumerator() e.EnumerateFacenames() fontList = e.GetFacenames()

To limit the list to only fixed-width, change the first line to e = wx.FontEnumera-tor(fixedWidth=True).

Was this article helpful?

+1 0

Post a comment