Home > OS >  AddPrivateFont to App Title / Title bar in WxPython?
AddPrivateFont to App Title / Title bar in WxPython?

Time:12-03

My problem is I can't find the way to use AddPrivateFont to change the App Title font in WxPython.

From the demo https://github.com/wxWidgets/Phoenix/blob/master/demo/AddPrivateFont.py

and the sample https://wiki.wxpython.org/How to add a menu bar in the title bar (Phoenix)

I tried

        f = wx.Font(
            pointSize=18,
            family=wx.FONTFAMILY_DEFAULT,
            style=wx.FONTSTYLE_NORMAL,
            weight=wx.FONTWEIGHT_NORMAL,
            underline=False,
            faceName="Youth Touch",
            encoding=wx.FONTENCODING_DEFAULT,
        )

        xwq = self.SetAppName("Custom Gui 1")
        xwq.SetFont(f)

But I'm getting the error:

AttributeError: 'NoneType' object has no attribute 'SetFont'

I also tested the same with SetTitle

        f = wx.Font(
            pointSize=18,
            family=wx.FONTFAMILY_DEFAULT,
            style=wx.FONTSTYLE_NORMAL,
            weight=wx.FONTWEIGHT_NORMAL,
            underline=False,
            faceName="Youth Touch",
            encoding=wx.FONTENCODING_DEFAULT,
        )

        frame.SetTitle('yourtitle')
        frame.SetFont(f)

and the same error shows up.

import wx
import wx.grid as grid

import os
import sys

try:
    gFileDir = os.path.dirname(os.path.abspath(__file__))
except:
    gFileDir = os.path.dirname(os.path.abspath(sys.argv[0]))
gDataDir = os.path.join(gFileDir, "myfonts")


filename = os.path.join(gDataDir, "YouthTouchDemoRegular-4VwY.ttf")
wx.Font.AddPrivateFont(filename)


class MyFrame(wx.Frame):
    def __init__(self, parent, title):
        super(MyFrame, self).__init__(parent, title=title, size=(800, 600))

        f = wx.Font(
            pointSize=12,
            family=wx.FONTFAMILY_DEFAULT,
            style=wx.FONTSTYLE_NORMAL,
            weight=wx.FONTWEIGHT_NORMAL,
            underline=False,
            faceName="Youth Touch DEMO Regular",
            encoding=wx.FONTENCODING_DEFAULT,
        )
        title.SetFont(f)

        self.panel = MyPanel(self)


class MyPanel(wx.Panel):
    def __init__(self, parent):
        super(MyPanel, self).__init__(parent)

        mygrid = grid.Grid(self)
        mygrid.CreateGrid(26, 9)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(mygrid, 1, wx.EXPAND)
        self.SetSizer(sizer)


class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(parent=None, title="Grid In WxPython")
        self.frame.Show()
        return True


app = MyApp()
app.MainLoop()

I's there a documented way or other known workaround example to set a Private Font to the Title?

EDIT:

Working result here from @Rob's solution :

Sample: https://web.archive.org/web/20221202192613/https://paste.c-net.org/HondoPrairie

try:
    gFileDir = os.path.dirname(os.path.abspath(__file__))
except:
    gFileDir = os.path.dirname(os.path.abspath(sys.argv[0]))
gDataDir = os.path.join(gFileDir, "myfonts")

print("gDataDir: ", gDataDir)

filename = os.path.join(gDataDir, "YouthTouchDemoRegular-4VwY.ttf")
wx.Font.AddPrivateFont(filename)

print("filename: ", filename)

self.label_font = self.GetFont()
self.label_font.SetPointSize(18)
self.label_font.SetFamily(wx.FONTFAMILY_DEFAULT)
self.label_font.SetStyle(wx.FONTSTYLE_NORMAL)
self.label_font.SetWeight(wx.FONTWEIGHT_NORMAL)
self.label_font.SetUnderlined(False)
self.label_font.SetFaceName("Youth Touch DEMO")
self.label_font.SetEncoding(wx.FONTENCODING_DEFAULT)
self.SetFont(self.label_font)

CodePudding user response:

The reason you are getting those errors is that the title for a frame is just a string.

To be able to modify the font in the title you will have to create a title bar class based off of wx.Control like they did in the sample. The created title bar will house the label and you will be able to set its font.

When you are ready to set the font run AddPrivateFont for it and then you should be able to reference it by faceName.

Also, the faceName you have above is not correct compared to the ttf in https://paste.c-net.org/FellasDerby . The faceName should be "Youth Touch DEMO"

From the docs modified with your font name/file:

filename = os.path.join(gDataDir, "YouthTouchDemoRegular-4VwY.ttf")
wx.Font.AddPrivateFont(filename)

st2 = wx.StaticText(self, -1, 'SAMPLETEXT', (15, 42))

f = wx.Font(pointSize=12,
    family=wx.FONTFAMILY_DEFAULT,
    style=wx.FONTSTYLE_NORMAL,
    weight=wx.FONTWEIGHT_NORMAL,
    underline=False,
    faceName="Youth Touch DEMO",
    encoding=wx.FONTENCODING_DEFAULT)

st2.SetFont(f)
  • Related