-
I'm using a custom font and it isn't a system font, therefore it can't be imported by its name. Is there a way to import a font from an TTF or OTF file ? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 10 replies
-
It is possible, however it is not integrated into tkinter or customtkinter, and is only possible by doing some hacks. Due to this, your font won't be platform independent. Here is the sample code to use external fonts on windows: # Note : You'd still have to download the font in your system
import pyglet
import customtkinter
pyglet.font.add_file('file.ttf')
root = customtkinter.CTk()
MyLabel = customtkinter.CTkLabel(root,text="test",font=('font name',25))
MyLabel.pack()
root.mainloop()
# This is not going to work on linux |
Beta Was this translation helpful? Give feedback.
-
There is a module called |
Beta Was this translation helpful? Give feedback.
-
@RVFET No need to use any other package/module like pyglet, it's inbuilt in customtkinter. customtkinter.FontManager.load_font("my_font.ttf")
# then simply use that font
button = customtkinter.CTkButton(app, font=("my_font",20)) Make sure that the name of the font is properly described. |
Beta Was this translation helpful? Give feedback.
-
I would like to share with u guys that the font name and name of the font file is different, so if anyone tries to import the font file and it dosent load the font style, then try using the font name. For eg. in the image below u can see the font name, i had imported the file but it was not loading the font so when i used the font name then only it loaded the font |
Beta Was this translation helpful? Give feedback.
-
Do you mean that on first run it only copies the font and in the second run
it loads it?
…On Thu, Sep 12, 2024 at 10:58 AM Khalmurad ***@***.***> wrote:
I have modified customtkinter/windows/widgets/font/font_manager.py to
allow for custom fonts to be loaded from files on Windows, macOS, and
Linux. Here is the code:
import sysimport osimport shutilfrom typing import Union
class FontManager:
linux_font_path = "~/.fonts/"
@classmethod
def init_font_manager(cls):
# Linux
if sys.platform.startswith("linux"):
try:
if not os.path.isdir(os.path.expanduser(cls.linux_font_path)):
os.mkdir(os.path.expanduser(cls.linux_font_path))
return True
except Exception as err:
sys.stderr.write("FontManager error: " + str(err) + "\n")
return False
# other platforms
else:
return True
@classmethod
def windows_load_font(cls, font_path: Union[str, bytes], private: bool = True, enumerable: bool = False) -> bool:
""" Function taken from: https://stackoverflow.com/questions/11993290/truly-custom-font-in-tkinter/30631309#30631309 """
from ctypes import windll, byref, create_unicode_buffer, create_string_buffer
FR_PRIVATE = 0x10
FR_NOT_ENUM = 0x20
if isinstance(font_path, bytes):
path_buffer = create_string_buffer(font_path)
add_font_resource_ex = windll.gdi32.AddFontResourceExA
elif isinstance(font_path, str):
path_buffer = create_unicode_buffer(font_path)
add_font_resource_ex = windll.gdi32.AddFontResourceExW
else:
raise TypeError('font_path 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 = add_font_resource_ex(byref(path_buffer), flags, 0)
return bool(min(num_fonts_added, 1))
@classmethod
def load_font(cls, font_path: str) -> bool:
# Windows
if sys.platform.startswith("win"):
return cls.windows_load_font(font_path, private=True, enumerable=False)
# Linux
elif sys.platform.startswith("linux"):
try:
shutil.copy(font_path, os.path.expanduser(cls.linux_font_path))
return True
except Exception as err:
sys.stderr.write("FontManager error: " + str(err) + "\n")
return False
# macOS
elif sys.platform.startswith("darwin"):
customtkinter_directory = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
font_path = os.path.join(customtkinter_directory, "assets", "fonts", "noto_sans", "regular.ttf")
try:
# Define the path to the Fonts folder in the user's home directory
fonts_folder = os.path.join(os.path.expanduser("~"), "Library", "Fonts")
# Check if the Fonts folder exists, if not, create it
if not os.path.exists(fonts_folder):
os.makedirs(fonts_folder)
# Copy the font file to the Fonts folder
shutil.copy(font_path, fonts_folder)
return True
except Exception as err:
sys.stderr.write("FontManager error: " + str(err) + "\n")
return False
# others
else:
return
Not sure if it will be too helpful unless @TomSchimansky
<https://github.com/TomSchimansky> adds it.
Hi!
I also had a problem with the font on MacOS, and I had a solution like the
one in the picture below:
image.png (view on web)
<https://github.com/user-attachments/assets/5f198593-0bc6-464c-a978-c2145142d0c1>
But there is a problem. The first time I run my program it doesn't read
custom fonts, the second time it does.
The custom font file is successfully copied on the first run of the
program. I checked from the fonts folder, the font file is there. But the
program doesn't show the custom font I need. The program reads the custom
font without any problems on the next launches.
Can you recommend any solution to this problem?
—
Reply to this email directly, view it on GitHub
<#900 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BA3K2F6VDKIOKUI23HGTP23ZWEQ7HAVCNFSM6AAAAAATDSVOA2VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTANRSGE3TENA>
.
You are receiving this because you commented.Message ID:
***@***.***
.com>
|
Beta Was this translation helpful? Give feedback.
@RVFET No need to use any other package/module like pyglet, it's inbuilt in customtkinter.
Make sure that the name of the font is properly described.