From 81599f643bae6867c29cf2006e6110b9f2265f5e Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Sun, 23 Jan 2022 06:39:46 -0500 Subject: [PATCH] Added Text.fonts_installed_list - returns the fonts installed as reported by tkinter. --- DemoPrograms/Demo_Font_Previewer.py | 19 +++++++++++-------- PySimpleGUI.py | 25 ++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/DemoPrograms/Demo_Font_Previewer.py b/DemoPrograms/Demo_Font_Previewer.py index 82fd33e1..84063b94 100644 --- a/DemoPrograms/Demo_Font_Previewer.py +++ b/DemoPrograms/Demo_Font_Previewer.py @@ -1,17 +1,20 @@ #!/usr/bin/env python -import sys import PySimpleGUI as sg -from tkinter import font -import tkinter -root = tkinter.Tk() -fonts = list(font.families()) -fonts.sort() -root.destroy() ''' - Showing fonts in PSG / tk + Demo Font Previewer + + Gets a list of the installed fonts according to tkinter. + Requires PySimpleGUI version 4.57.0 and newer (that's when sg.Text.fonts_installed_list was added) + + Uses the Text element's class method to get the fonts reported by tkinter. + + Copyright 2020, 2021, 2022 PySimpleGUI ''' +fonts = sg.Text.fonts_installed_list() + + sg.theme('Black') layout = [[sg.Text('My Text Element', diff --git a/PySimpleGUI.py b/PySimpleGUI.py index 2b3442f2..81d4e08b 100644 --- a/PySimpleGUI.py +++ b/PySimpleGUI.py @@ -1,5 +1,5 @@ #!/usr/bin/python3 -version = __version__ = "4.56.0.8 Unreleased" +version = __version__ = "4.56.0.9 Unreleased" _change_log = """ Changelog since 4.56.0 released to PyPI on 5-Jan-2022 @@ -22,6 +22,8 @@ _change_log = """ 4.56.0.8 Changed +CICKED+ to +CLICKED+ (typo) in the table header Added constant TABLE_CLICKED_INDICATOR that is the value '+CLICKED+' so that it can be referenced instead of user's hard cording a string + 4.56.0.9 + Added class method Text.fonts_installed_list - returns list of fonts as reported by tkinter """ __version__ = version.split()[0] # For PEP 396 and PEP 345 @@ -3470,6 +3472,27 @@ class Text(Element): return text + @classmethod + def fonts_installed_list(cls): + """ + Returns a list of strings that tkinter reports as the installed fonts + + :return: List of the installed font names + :rtype: List[str] + """ + # if no windows have been created (there is no hidden master root to rely on) then temporarily make a window so the measurement can happen + if Window.hidden_master_root is None: + root = tk.Tk() + else: + root = Window.hidden_master_root + + fonts = list(tkinter.font.families()) + fonts.sort() + if Window.hidden_master_root is None: + root.destroy() + return fonts + + @classmethod def char_width_in_pixels(cls, font, character='W'): """