TomSchimansky / CustomTkinter

A modern and customizable python UI-library based on Tkinter
MIT License
11.33k stars 1.07k forks source link

FontManager Loaded Font Does Not Render Correct #2537

Closed RPINerd closed 1 month ago

RPINerd commented 1 month ago

So I have this font as a .ttf file, here is the 'preview' of what it looks like: FontPreview

I am fairly certain that I've used the code correctly but as you can see in the window, the rendered font looks like just the system default:

import customtkinter as ctk

window = ctk.CTk()
window.title("Font Test")
window.geometry("400x400")

# Load the fonts
ctk.FontManager.load_font("assets/fonts/palm-pilot-small.ttf")

# Create a label with the custom font
label = ctk.CTkLabel(window, text="Hello, World!", font=("palm-pilot-small", 36))
label.pack(fill="both", expand=True)

window.mainloop()

Window

Akascape commented 1 month ago

@RPINerd

Possible fix:

1) The import name for that font is different, try to find the correct font name 2) You should try passing the ctk,CTkFont("palm-pilot-small", 36)

A font import example program for your reference:

# Author: Akash Bora

import customtkinter
from tkinter import filedialog
import os

def load_font():
    global font
    if file := filedialog.askopenfilename(
        filetypes=[("Font files", ["*.ttf", "*.otf"]),("All Files", "*.*")]):
            customtkinter.FontManager.load_font(file) # load font

            font_name = os.path.basename(file).split(".")[0] # get font_name, can be different in some case

            font = customtkinter.CTkFont(font_name,int(slider.get()))
            label.configure(font=font)
            button.configure(text=font_name)

root = customtkinter.CTk()
root.geometry("500x150")
font = customtkinter.CTkFont(customtkinter.ThemeManager.theme["CTkFont"])
font.configure(size=30)

button = customtkinter.CTkButton(root, text="Import Font", command=load_font)
button.pack(padx=10, pady=10, fill="x", expand=True)

slider = customtkinter.CTkSlider(root, from_=10, to=100, command=lambda e:font.configure(size=int(e)))
slider.pack(padx=10, pady=0, fill="x", expand=True)

label = customtkinter.CTkEntry(root, border_width=0, fg_color="transparent", font=font)
label.insert(0, "This is default text")
label.pack(padx=10, pady=10, fill="both", expand=True)

slider.set(30)

root.mainloop()
RPINerd commented 1 month ago

Thanks for getting back to me! Sadly it looks like the result is the same, the computer itself can preview the font, but when loaded into ctk it doesn't render correctly..

image

I can provide the font file if that would help, it's also available on it's own repo in my account: https://github.com/RPINerd/PalmOS-Fonts/tree/main/Fonts

Akascape commented 1 month ago

@RPINerd Can be a problem with the name of the font. The import name is different I guess. try palm-pilot or palm_pilot

RPINerd commented 1 month ago

I'll try out a few different combos after lunch and get back to you, did a print of the ctkfont name property and it was returning 'font1' so maybe the TTF files don't have a proper name associated with the font..

RPINerd commented 1 month ago

@Akascape You were right! The actual names were camel-cased with spaces, which I did not expect, it looks like they are rendering as expected now, thank you!