PySimpleGui: change font, font display, Ubuntu, ugly fonts
Asked Answered
D

3

6

Out of the box it seems that the demo app examples for PySimpleGui display with an "ugly" font when using Linux (Ubuntu 20.10).

As I cannot find any references how to control this in the demo examples provided, such as default_font = 'Helvetica'), it seems, implicitly, those examples assumes the default font setting should already be correct.

To try solving it, I have installed Helvetica and default Windows fonts, but it is still showing different to examples depicted online.

Below example is obviously not Helvetica.

enter image description here

How do I solve this?

import PySimpleGUI as sg

'''
    App that shows "how fonts work in PySimpleGUI".
'''

layout = [[sg.Text('This is my sample text', size=(20, 1), key='-text-')],
          [sg.CB('Bold', key='-bold-', change_submits=True),
           sg.CB('Italics', key='-italics-', change_submits=True),
           sg.CB('Underline', key='-underline-', change_submits=True)],
          [sg.Slider((6, 50), default_value=12, size=(14, 20),
                     orientation='h', key='-slider-', change_submits=True),
           sg.Text('Font size')],
          [sg.Text('Font string = '), sg.Text('', size=(25, 1), key='-fontstring-')],
          [sg.Button('Exit')]]

window = sg.Window('Font string builder', layout)

text_elem = window['-text-']
while True:     # Event Loop
    event, values = window.read()
    if event in (sg.WIN_CLOSED, 'Exit'):
        break
    font_string = 'Helvitica '
    font_string += str(int(values['-slider-']))
    if values['-bold-']:
        font_string += ' bold'
    if values['-italics-']:
        font_string += ' italic'
    if values['-underline-']:
        font_string += ' underline'
    text_elem.update(font=font_string)
    window['-fontstring-'].update('"'+font_string+'"')
    print(event, values)

window.close()

Update: In addition the below answer, it also seems to be a known problem if you use an Anaconda / conda environment. I deleted Anaconda from my system and ran a pipenv environment instead, and it worked.

I noticed it when I ran below code and hardly any fonts where showing, meanwhile in a 'normal' environment it matches FontManager's list of fonts.

from tkinter import Tk, font

root = Tk()
font_tuple = font.families()
root.destroy()
for font in font_tuple:
    print(font)
Dimer answered 18/4, 2021 at 12:51 Comment(2)
You misspelled Helvetica in your code - I assume a non-existent font name is automatically replaced with some default font.Expression
A call to sg.Text.fonts_installed_list() will return a list of the font families so you don't need to call tkinter directly. It's a class method so don't need to do anything but make the call directly.Petrolatum
C
13

There're four ways to set the font for element(s)

  • Add option font to element
font = ("Arial", 11)
sg.Text('This is my sample text', size=(20, 1), key='-text-', font=font)
  • Update element by option font in method update of element sg.Text.
window['-text-'].update(font=font)
  • Set default font by option font in sg.Window
window = sg.Window('Font string builder', layout, font=font)
  • Set default font by option font in method sg.set_options before layout
sg.set_options(font=font)
layout = [[sg.Text('This is my sample text', size=(20, 1), key='-text-')],
...

Default font sg.DEFAULT_FONT is ("Helvetica", 11). Font maybe not exist in your system, then another tkinter default font will be used.

To make sure what font exist in your system, following code will show all of fonts.

from tkinter import Tk, font

root = Tk()
font_tuple = font.families()
root.destroy()
for font in font_tuple:
    print(font)

or

import PySimpleGUI as sg

for font in sg.Text.fonts_installed_list():
    print(font)
Chiquita answered 19/4, 2021 at 3:54 Comment(0)
C
2

This code lists all the fonts on your system, works on Pydroid Android and uses PySimpleGUI. Modified from above the answer which produces a black screen on my phone.

from tkinter import Tk, font
import  PySimpleGUI as sg
root = Tk()
font_tuple = font.families()
root.destroy()
#Creates a Empty list to hold font names
FontList=[]
for font in font_tuple:
    FontList.append(font)
#size 28, 28 is optimized for my Android phone please tweak as per your screen
#Scrolled popup to accommodate big list
sg.popup_scrolled(FontList, title='All fonts installed using PySimpleGUI', size=(28,28), grab_anywhere=True)

List of all fonts on Samsung A30 Pydroid interpreter

Clinandrium answered 1/2, 2022 at 14:20 Comment(0)
I
0

Here is a simple PySimpleGui program that demonstrates listing all the system fonts and applying any of the 4 style modifiers (Bold, Italics, Underline, Overstrike/Strikeout).

This is a basically a combination of everything that everyone has posted before, plus the style modifier of Overstrike.

This starts with the default PysimpleGui font & size for your system.

import PySimpleGUI as sg
layout = [[sg.Text('This is my sample text', size=(30, 1), key='-text-')],
          [sg.T('Font:'),sg.Combo(sg.Text.fonts_installed_list(),default_value=sg.DEFAULT_FONT[0],
                                  key='-font-',  enable_events=True)],
          [sg.CB('Bold', key='-bold-', change_submits=True),
           sg.CB('Italics', key='-italics-', change_submits=True),
           sg.CB('Underline', key='-underline-', change_submits=True),
           sg.CB('OverStrike', key='-overstrike-', change_submits=True)],
          [sg.Slider((6, 50), default_value=sg.DEFAULT_FONT[1], size=(14, 20),
                     orientation='h', key='-slider-', change_submits=True),
           sg.Text('Font size')],
          [sg.Text('Font string = '), sg.Text(f'{sg.DEFAULT_FONT}', size=(50, 1), key='-fontstring-')],
          [sg.Button('Exit')]]

window = sg.Window('Font string builder', layout)

text_elem = window['-text-']
while True:     # Event Loop
    event, values = window.read()
    if event in (sg.WIN_CLOSED, 'Exit'):
        break
    values['-font-'] + " "
    str(int(values['-slider-']))
    style = ""
    if values['-bold-']:
        style += 'bold '
    if values['-italics-']:
        style += 'italic '
    if values['-underline-']:
        style += 'underline '
    if values['-overstrike-']:
        style += 'overstrike '
    newFont = (values['-font-'],"{:.0f}".format(values['-slider-']),style)
    #text_elem.update(font=newFont)
    window['-text-'].update(font=newFont)
    window['-fontstring-'].update(value = str(newFont))

window.close()

Please mark the answer as useful, if you like it. Thanks.

Implosion answered 24/6, 2023 at 2:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.