Using Fonts

Simple program that writes text to a panel.

Code Sample

   1 import wx
   3 class DrawPanel(wx.Panel):
   4     def __init__(self, parent):
   5         wx.Panel.__init__(self, parent)
   6         self.Bind(wx.EVT_PAINT, self.OnPaint)
   8     def OnPaint(self, event=None):
   9         dc = wx.PaintDC(self)
  10         dc.Clear()
  11         dc.SetFont(wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL))
  12         dc.DrawText("Modern font Family", 50, 50)
  14 app = wx.App(0)
  15 frame = wx.Frame(None, title="Font Family")
  16 DrawPanel(frame)
  17 frame.Show()
  18 app.MainLoop()


You can use operating system fonts, if you are not concerned about platform independence or you can also access the system fonts by using wxfontDialog.

Custom Fonts on Windows

To load custom fonts on Windows use the following recipe:

   1 from ctypes import *
   2 import os
   3 import os.path
   4 gdi32= WinDLL("gdi32.dll")
   5 fonts = [font for font in os.listdir("fonts") if font.endswith("otf") or font.endswith("ttf")]
   6 for font in fonts:
   7     gdi32.AddFontResourceA(os.path.join("fonts",font))

This will load all .otf and .ttf fonts in the "fonts" subdirectory of your project.

UsingFonts (last edited 2008-03-11 10:50:18 by localhost)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.