Applying a common font scheme to multiple objects in wxPython

Many times I will use the same font scheme for static text in a wxPython application. I am currently doing SetFont()for each static text object, but this seems like a lot of unnecessary work. However, the wxPython demo and the wxPython In Action book do not discuss this.

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

+3
source share
4 answers

You can do this by calling SetFont in the parent window (Frame, Dialog, etc.) before adding any widgets. Baby widgets inherit the font.

+5
source

, , __init__ SetFont()?

- :

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

, :

text = f(wx.StaticText)

(, StaticText , f).

+1

, SetFont, , :

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)

, frame :

newFont = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
newFont.SetStyle(wx.FONTSTYLE_ITALIC)
changeFontInChildren(frame, newFont)
0

, @DzinX, Panel, .

, ( AuiManager ).

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
0

Source: https://habr.com/ru/post/1697909/


All Articles