views:

440

answers:

3

Many times I will use the same font scheme for static text in a wxPython application. Currently I am making a SetFont() call for each static text object but that seems like a lot of unnecessary work. However, the wxPython demo and wxPython In Action book don't discuss this.

Is there a way to easily apply the same SetFont() method to all these text objects without making separate calls each time?

+1  A: 

Maybe try subclassing the text object and in your class __init__ method just call SetFont()?

Or, do something like:

def f(C):
  x = C()
  x.SetFont(font) # where font is defined somewhere else
  return x

and then just decorate every text object you create with with it:

text = f(wx.StaticText)

(of course, if StaticText constructor requires some parameters, it will require changing the first lines in f function definition).

kender
A: 

If all widgets have already been created, you can apply SetFont recursively, for example with the following function:

def changeFontInChildren(win, font):
    '''
    Set font in given window and all its descendants.
    @type win: L{wx.Window}
    @type font: L{wx.Font}
    '''
    try:
        win.SetFont(font)
    except:
        pass # don't require all objects to support SetFont
    for child in win.GetChildren():
        changeFontInChildren(child, font)

An example usage that causes all text in frame to become default font with italic style:

newFont = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
newFont.SetStyle(wx.FONTSTYLE_ITALIC)
changeFontInChildren(frame, newFont)
DzinX
+4  A: 

You can do this by calling SetFont on the parent window (Frame, Dialog, etc) before adding any widgets. The child widgets will inherit the font.

giltay