From c277fc48bc0537f8c76149f44129e51b4a3d27fb Mon Sep 17 00:00:00 2001 From: TomSchimansky Date: Tue, 5 Apr 2022 23:23:20 +0200 Subject: [PATCH] removed pyglet and added new font loading method --- customtkinter/__init__.py | 33 ++++++++++++++++++++++++--------- test/test_askdialog.py | 2 +- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/customtkinter/__init__.py b/customtkinter/__init__.py index a21eefa..c53ab34 100644 --- a/customtkinter/__init__.py +++ b/customtkinter/__init__.py @@ -65,21 +65,36 @@ def set_default_color_theme(color_string): CTkThemeManager.load_theme(color_string) +# Load fonts: if sys.platform.startswith("win"): - import warnings + from ctypes import windll, byref, create_unicode_buffer, create_string_buffer - warnings.simplefilter("ignore", category=UserWarning) + FR_PRIVATE = 0x10 + FR_NOT_ENUM = 0x20 + + + def loadfont(fontpath, private=True, enumerable=False): + """ Function taken from: https://stackoverflow.com/questions/11993290/truly-custom-font-in-tkinter/30631309#30631309 """ + + if isinstance(fontpath, bytes): + pathbuf = create_string_buffer(fontpath) + AddFontResourceEx = windll.gdi32.AddFontResourceExA + elif isinstance(fontpath, str): + pathbuf = create_unicode_buffer(fontpath) + AddFontResourceEx = windll.gdi32.AddFontResourceExW + else: + raise TypeError('fontpath must be of type str or unicode') + + flags = (FR_PRIVATE if private else 0) | (FR_NOT_ENUM if not enumerable else 0) + num_fonts_added = AddFontResourceEx(byref(pathbuf), flags, 0) + return bool(num_fonts_added) - import pyglet.font # load text fonts and custom font with circle shapes for round corner rendering script_directory = os.path.dirname(os.path.abspath(__file__)) - pyglet.font.add_file(os.path.join(script_directory, "assets", "fonts", "CustomTkinter_shapes_font-fine.otf")) - pyglet.font.add_file(os.path.join(script_directory, "assets", "fonts", "Roboto", "Roboto-Regular.ttf")) - pyglet.font.add_file(os.path.join(script_directory, "assets", "fonts", "Roboto", "Roboto-Medium.ttf")) - CTkSettings.circle_font_is_ready = pyglet.font.have_font("CustomTkinter_shapes_font") - - warnings.simplefilter("default") + CTkSettings.circle_font_is_ready = loadfont(os.path.join(script_directory, "assets", "fonts", "CustomTkinter_shapes_font-fine.otf")) + loadfont(os.path.join(script_directory, "assets", "fonts", "Roboto", "Roboto-Regular.ttf")) + loadfont(os.path.join(script_directory, "assets", "fonts", "Roboto", "Roboto-Medium.ttf")) # correct drawing method if font could not be loaded if not CTkSettings.circle_font_is_ready: diff --git a/test/test_askdialog.py b/test/test_askdialog.py index db647c6..a4c54c1 100644 --- a/test/test_askdialog.py +++ b/test/test_askdialog.py @@ -47,7 +47,7 @@ class App(customtkinter.CTk): self.button_output = customtkinter.CTkButton(master=self.frame_right, border_color=App.MAIN_COLOR, fg_color=None, hover_color=App.MAIN_HOVER, height=28, text="Output Folder", command=self.button_outputFunc, - border_width=3, corner_radius=10, text_font=('Calibri',16)) + border_width=3, corner_radius=10, text_font=('Calibri',12)) self.button_output.place(relx=0.05, rely=0.06, anchor=tkinter.NW) self.entry_output = customtkinter.CTkEntry(master=self.frame_right, width=320, height=38, corner_radius=5) self.entry_output.place(relx=0.05, rely=0.18, anchor=tkinter.NW)