Reputation: 5089
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?
Upvotes: 3
Views: 1663
Reputation: 381
The solution given above by @DzinX worked for me when changing the font dynamically in a Panel that already had children and was already being shown.
I ended up modifying it as follows because the original gave me trouble in corner cases (i.e. when using an AuiManager
with Floating frames).
def change_font_in_children(win, font):
'''
Set font in given window and all its descendants.
@type win: L{wx.Window}
@type font: L{wx.Font}
'''
for child in win.GetChildren():
change_font_in_children(child, font)
try:
win.SetFont(font)
win.Update()
except:
pass # don't require all objects to support SetFont
Upvotes: 0
Reputation: 2025
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.
Upvotes: 5
Reputation: 57874
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)
Upvotes: 0
Reputation: 87271
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).
Upvotes: 1