Lasse Lang
Lasse Lang

Reputation: 43

PySimpleGUI Monospace font

How can I create a Multiline in PySimpleGUI which uses a monospaced font?

sg.Multiline(
 "Test",
 expand_x=True,
 expand_y=True,
 key="-TEXT-",
 background_color="black",
 text_color="white",
 no_scrollbar=True
)

I have this element and I tried to change the font with

font=("Andale Mono", 12)

to monospace but it isn't monospaced.

Upvotes: 1

Views: 1166

Answers (1)

Jason Yang
Jason Yang

Reputation: 13061

Try to use option font when initiate Elements, function sg.set_options or Window class.

Be sure to confirm the font "Andale Mono" exist in the list sg.Text.fonts_installed_list() before you use it.

import string
import PySimpleGUI as sg

font1 = ("Andale Mono", 20)
font2 = ("Courier New", 20)
font3 = ("Arial", 20, 'bold italic')

sg.set_options(font=font3)

layout = [
    [sg.Multiline(string.ascii_lowercase, size=(40, 1), font=font1)],
    [sg.Multiline(string.ascii_lowercase, size=(40, 1), font=font2)],
    [sg.Multiline(string.ascii_lowercase, size=(40, 1))],
]
window = sg.Window('Title', layout, finalize=True)
print("Andale Mono" in sg.Text.fonts_installed_list())
window.read(close=True)

enter image description here

Cases for font, including font family and font size

  • If not specified or None in PySimpleGUI, sg.DEFAULT_FONT will be used.
  • If specified, font exists in tkinter, then use it.
  • If specified, font not exists in tkinter, then tkinter.font.nametofont("TkDefaultFont") will be used, use the size even if wrong font family specified and also size specified.

In your case, it is most possible that "Andale Mono" not installed in your system/platform, so another default font used for it.

Upvotes: 3

Related Questions