I want to know how to dynamically load font files at runtime?
Version of Dear PyGui
Version: 1.11 Operating System: Windows 11
My Issue/Question
I want to know how to dynamically load font files at runtime, such as allowing me to freely choose to load new font files and then preview the newly loaded fonts. By the way, I started using a font as the default font.
To Reproduce
Steps to reproduce the behavior:
- Go to '...'
- Click on '....'
- Scroll down to '....'
- See error
Expected behavior
A clear and concise description of what you expected to happen.
Screenshots/Video
XXX (you can drag files here)
Standalone, minimal, complete and verifiable example
# Here's some code anyone can copy and paste to reproduce your issue
import dearpygui.dearpygui as dpg
dpg.create_context()
dpg.create_viewport()
dpg.setup_dearpygui()
with dpg.window(label="tutorial"):
dpg.add_button(label="Press me")
dpg.add_input_text('font_file',default_value="path/", width=129)
dpg.add_input_int(label='px', tag='size',default_value=20, width=129)
with dpg.child_window(tag="window"):pass
def change():
font = dpg.get_value('font_file')
size = dpg.get_value('size')
__font_tag = f"{font}, {size}"
print(self.__font_tag)
with dpg.font_registry():
with dpg.font(f"./font_data/fonts/{font}", size,tag=__font_tag):
dpg.add_font_range_hint(dpg.mvFontRangeHint_Chinese_Full)
dpg.add_font_range_hint(dpg.mvFontRangeHint_Chinese_Simplified_Common)
dpg.add_font_range_hint(dpg.mvFontRangeHint_Japanese)
dpg.add_font_range_hint(dpg.mvFontRangeHint_Cyrillic)
dpg.add_font_range_hint(dpg.mvFontRangeHint_Korean)
dpg.add_font_range_hint(dpg.mvFontRangeHint_Thai)
dpg.add_font_range_hint(dpg.mvFontRangeHint_Vietnamese)
txt_list = ["text the font","测试文本"]
for i in txt_list:
f = dpg.add_text(i,parent='window',tag=i)
dpg.bind_item_font(f,__font_tag)
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
not using font hints, but here's a simple example for a font viewer that might help ?
import dearpygui.dearpygui as dpg
dpg.create_context()
dpg.create_viewport(title='Font viewer example', width=600, height=400)
dpg.add_font_registry(tag='font_reg')
current_font_name = None
available_font_sizes = [14, 50, 90]
def add_font(sender, app_data):
global current_font_name, available_font_sizes
# get font name and path
font = app_data['file_path_name']
font_name = app_data['file_name']
current_font_name = font_name
dpg.set_value('font_path', font)
# add to font registry, in multiple sizes as in "available_font_sizes"
added_fonts = []
for size in available_font_sizes:
font_tag = f'{font_name} : {size}'
if not dpg.does_item_exist(font_tag):
dpg.add_font(file=font, tag=font_tag, size=size, parent='font_reg')
added_fonts.append(font_tag)
# set sample text
sample_text = dpg.get_value('sample_text')
size_index = dpg.get_value('font_size')-1
dpg.bind_item_font('sample_text_with_chosen_font', added_fonts[size_index])
dpg.set_value('sample_text_with_chosen_font', sample_text)
def change_font_size(sender, app_data):
global current_font_name, available_font_sizes
size = available_font_sizes[dpg.get_value('font_size')-1]
font_tag = f'{current_font_name} : {size}'
dpg.bind_item_font('sample_text_with_chosen_font', font_tag)
dpg.set_value('size_value', f'{size} px')
def change_sample_text(sender, app_data):
sample_text = dpg.get_value('sample_text')
dpg.set_value('sample_text_with_chosen_font', sample_text)
with dpg.window(tag='main_window', label='Font viewer'):
# create font chooser dialog as hidden
with dpg.file_dialog(tag='font_dialog', label='Choose font', width=400, height=200, show=False, callback=add_font):
dpg.add_file_extension("Font files (*.ttf *.otf}{.ttf,.otf}")
# choose font file
with dpg.group(horizontal=True):
dpg.add_text('Font:')
dpg.add_input_text(tag='font_path')
dpg.add_button(label='Choose...', callback=lambda:dpg.show_item('font_dialog'))
# set font size
with dpg.group(horizontal=True):
dpg.add_text('Size:')
dpg.add_slider_int(tag='font_size', default_value=int(len(available_font_sizes)/2), min_value=1, max_value=len(available_font_sizes), callback=change_font_size)
dpg.add_text(f'{available_font_sizes[dpg.get_value("font_size")-1]} px', tag='size_value')
# sample text to preview font
dpg.add_separator()
with dpg.group(horizontal=True):
dpg.add_text('Sample text:')
dpg.add_input_text(tag='sample_text', default_value='My Text!', callback=change_sample_text)
dpg.add_text(tag='sample_text_with_chosen_font')
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.set_primary_window('main_window', True)
dpg.start_dearpygui()
dpg.destroy_context()
Thank you for your suggestion, but unfortunately, just tested it and it worked when I didn't set the initial font. Once I do set the initial font, it will crash again.