created FontManager and restructured __init__.py

This commit is contained in:
Tom Schimansky
2022-05-17 21:44:59 +02:00
parent eec16e4509
commit afe44f4ff7
23 changed files with 436 additions and 430 deletions

View File

@@ -1,5 +1,39 @@
__version__ = "3.12"
import os
import sys
# import manager classes
from .settings import Settings
from .appearance_mode_tracker import AppearanceModeTracker
from .theme_manager import ThemeManager
from .scaling_tracker import ScalingTracker
from .font_manager import FontManager
Settings.init_font_character_mapping()
Settings.init_drawing_method()
AppearanceModeTracker.init_appearance_mode()
ThemeManager.load_theme("blue")
FontManager.init_font_manager()
# load Roboto fonts
script_directory = os.path.dirname(os.path.abspath(__file__))
FontManager.load_font(os.path.join(script_directory, "assets", "fonts", "Roboto", "Roboto-Regular.ttf"))
FontManager.load_font(os.path.join(script_directory, "assets", "fonts", "Roboto", "Roboto-Medium.ttf"))
# load font necessary for rendering the widgets on Windows, Linux
if FontManager.load_font(os.path.join(script_directory, "assets", "fonts", "CustomTkinter_shapes_font-fine.otf")) is True:
Settings.circle_font_is_ready = True
if Settings.preferred_drawing_method == "font_shapes":
sys.stderr.write("customtkinter.__init__ warning: " +
"Preferred drawing method 'font_shapes' can not be used because the font file could not be loaded.\n" +
"Using 'circle_shapes' instead. The rendering quality will be bad!")
Settings.preferred_drawing_method = "circle_shapes"
# import widgets
from .widgets.ctk_button import CTkButton
from .widgets.ctk_checkbox import CTkCheckBox
@@ -17,16 +51,6 @@ from .windows.ctk_tk import CTk
from .windows.ctk_toplevel import CTkToplevel
from .windows.ctk_input_dialog import CTkInputDialog
# import other classes
from .ctk_settings import CTkSettings
from .appearance_mode_tracker import AppearanceModeTracker
from .ctk_theme_manager import CTkThemeManager
from .scaling_tracker import ScalingTracker
import os
import sys
import shutil
def set_appearance_mode(mode_string):
AppearanceModeTracker.set_appearance_mode(mode_string)
@@ -40,77 +64,13 @@ def get_appearance_mode():
def set_default_color_theme(color_string):
CTkThemeManager.load_theme(color_string)
ThemeManager.load_theme(color_string)
def deactivate_dpi_awareness(deactivate_awareness: bool):
CTkSettings.deactivate_automatic_dpi_awareness = deactivate_awareness
Settings.deactivate_automatic_dpi_awareness = deactivate_awareness
def set_user_scaling(scaling_value: float):
ScalingTracker.set_spacing_scaling(scaling_value)
ScalingTracker.set_widget_scaling(scaling_value)
# Load fonts:
if sys.platform.startswith("win"):
from ctypes import windll, byref, create_unicode_buffer, create_string_buffer
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 bytes or str')
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)
# load text fonts and custom font with circle shapes for round corner rendering
script_directory = os.path.dirname(os.path.abspath(__file__))
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:
if CTkSettings.preferred_drawing_method == "font_shapes":
sys.stderr.write("WARNING (customtkinter.CTkSettings): " +
"Preferred drawing method 'font_shapes' can not be used because the font file could not be loaded.\n" +
"Using 'circle_shapes' instead. The rendering quality will be very bad!")
CTkSettings.preferred_drawing_method = "circle_shapes"
elif sys.platform.startswith("linux"):
try:
if not os.path.isdir(os.path.expanduser('~/.fonts/')):
os.mkdir(os.path.expanduser('~/.fonts/'))
script_directory = os.path.dirname(os.path.abspath(__file__))
# copy fonts in user font folder
shutil.copy(os.path.join(script_directory, "assets", "fonts", "Roboto", "Roboto-Regular.ttf"),
os.path.expanduser("~/.fonts/"))
shutil.copy(os.path.join(script_directory, "assets", "fonts", "Roboto", "Roboto-Medium.ttf"),
os.path.expanduser("~/.fonts/"))
shutil.copy(os.path.join(script_directory, "assets", "fonts", "CustomTkinter_shapes_font-fine.otf"),
os.path.expanduser("~/.fonts/"))
except Exception as err:
sys.stderr.write(str(err) + "\n")
sys.stderr.write("WARNING (customtkinter.CTkSettings): " +
"Preferred drawing method 'font_shapes' can not be used because the font file could not be copied to ~/.fonts/.\n" +
"Using 'circle_shapes' instead. The rendering quality will be very bad!\n")
CTkSettings.preferred_drawing_method = "circle_shapes"