dev
dev

Reputation: 109

PySimpleGUI: Font rendering issue

I'm using a font on my dev machine, however when I bundle the application as an exe and deploy it on another machine which does not have that font installed, the font rendering shifts to default. Is there a way I can bundle the font with the exe and have PySimpleGUI use that instead of trying to locate the font in the system (implying it needs to be installed first)? Any workaround for this?

import PySimpleGUI as sg    
sg.set_options(font=['Inder',10]) 

Upvotes: 0

Views: 657

Answers (1)

Jason Yang
Jason Yang

Reputation: 13061

You can use library pyglet to add font files, and use option --add-data <SRC;DEST or SRC:DEST> of PyInstaller to bundle the font files.

Additional non-binary files or folders to be added to the executable. The path separator is platform specific, os.pathsep (which is ; on Windows and : on most unix systems) is used. This option can be used multiple times.

[Update] There's issue to work with tkinter.filedialog module. Set COINIT_APARTMENTTHREADED mode before your code.

Demo code

import pyglet
from pyglet.libs.win32 import constants
import PySimpleGUI as sg


constants.COINIT_MULTITHREADED = 0x2  # 0x2 = COINIT_APARTMENTTHREADED

# pyglet.font.add_file(r".\MerryChristmasFlake.ttf")
# pyglet.font.add_file(r".\MerryChristmasStar.ttf")

sg.theme("DarkBlue3")
font1 = ("Merry Christmas Flake", 40)
font2 = ("Merry Christmas Star", 40)

layout = [
    [sg.Text("Merry Christmas Flake", font=font1)],
    [sg.Text("Merry Christmas Star",  font=font2)],
    [sg.Input(), sg.FolderBrowse()],
]

window = sg.Window('Title', layout, finalize=True)

while True:

    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    print(event, values)

window.close()

enter image description here

After two remark lines unmarked, enter image description here

Note: font file downloaded from https://www.1001freefonts.com/d/17982/merry-christmas.zip, and put those two font files to same path as main script.

Upvotes: 1

Related Questions