Main Menu

search

You are here

Guizero: custom fonts:

[last updated: 2025-10-06]
Disclaimers
GUIzero home page
----------



      On This Page:
  • .otf fonts
  • ...
  • ...


---------------------------------------------------------

  • .OTF Fonts:
    • ".otf" are "open-type" fonts
    • download a .otf file you find online for the font you want
    • Install it onto your OS:
      (guizero uses tkinter, which uses fonts available to the underlying graphical system, ie. your OS)
        I don't understand why you do it this way, but this is what the online instructions said to do.
        You copy the .otf file into two directories, which you need to create if they don't already exist.
        The two directories are: /usr/share/fonts/custom, and ~/.fonts
      • Install the font on the rPi:
        It must be in a directory where fontconfig can find it.
        Start by creating a personal directory for your custom fonts.
        This new custom folder will be located in the standard fonts directory, which is /usr/share/fonts
          $ sudo mkdir /usr/share/fonts/custom ... "custom" is your choice of name for your personal directory

        Copy the new font .otf file that you downloaded from wherever into this new custom directory.

          $ sudo cp [fontFile.otf] /usr/share/fonts/custom/


          Create a hidden directory in your home folder, named .fonts.

            $ sudo mkdir ~/.fonts

          Copy your custom .otf file into this directory:

            $ cd /usr/share/fonts/custom/
            $ cp [fontFile.otf] ~/.fonts/

        --------------------

      • Rebuild the font cache:
        • This is necessary for guizero to find the new font.
            $ fc-cache -f -v

          The -f flag forces a rebuild, and -v provides verbose output so you can see the cache being rebuilt.

        • reboot

      --------------------------------------------------

    • Use the new font in guizero:
      • Briefly, you'll use the new font to style a text parameter for some widget in your GUI.
        There are two steps to this: declare the new font, then assign it to the text you want to style.
      • Declare the font in your code:
        import tkinter as tk (the import line can be near the top of your code as usual)
        customFont = tk.font.Font(family="[font family]", size=20, weight="bold")
            Gotcha: This declaration line must be in your code after you've created your main App window.
          Regarding the "font family":
          This is not intuitive/obvious. The "family" is NOT the filename.
          In order to find the font family name:
            In terminal, execute:
              # fc-list

            Scroll through the long list that is output from this command to find the lines relating to the new font you've installed.
            The output lines will be of the form:

              /usr/share/fonts/TTF/LiberationMono-Italic.ttf: Liberation Mono:style=Italic
              /usr/share/fonts/noto/NotoSansTaiLe-Regular.ttf: Noto Sans Tai Le:style=Regular

            The "font family name" in the above two example lines are:

              Liberation Mono
              and Noto Sans Tai Le

            That is, the text after the ".ttf:" that defines the path, and before the ":style" text.

      • Create your widget:
        You can use any widget that accepts a text parameter, the simplest being a Text widget:
          textDemo = Text(App, text="Hello, world")
      • Style the text with your new font:
          textDemo.tk.configure(font=customFont)
      • -or- create widget and style it in one command:
          textDemo = Text(App, text="Hello, World", font="[font family name]", size=20)

      ---------------------------------------------------------

    • Notes:
      You might need to restart your guizero application or even the Raspberry Pi for the font changes to fully take effect.

    -----------------------------------------


    eof