我能够重现您的错误的一种方法是使用错误的文件名。我怀疑您的“Broken Glass.ttf”文件名称中包含拼写错误,或者不在项目的根文件夹中,在这种情况下,您应该使用 .ttf 文件的完整路径。
这是一个 glfw + imgui 实现的示例,它具有对我有用的不同字体。您的代码在 start() 中:
import glfw
import OpenGL.GL as gl
import imgui
from imgui.integrations.glfw import GlfwRenderer
window_width = 960
window_height = 540
window_name = "glfw / imgui window"
def glfw_init():
if not glfw.init():
print("Could not initialize OpenGL context.")
exit(1)
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE)
window = glfw.create_window(window_width, window_height, window_name, None, None);
glfw.make_context_current(window)
if not window:
glfw.terminate()
print("Could not initialize Window")
exit(1)
return window
def start():
io = imgui.get_io()
io.fonts.clear()
io.font_global_scale = 1
new_font = io.fonts.add_font_from_file_ttf("Roboto-Regular.ttf", 20.0, io.fonts.get_glyph_ranges_latin())
impl.refresh_font_texture()
def onupdate():
imgui.begin("Test window")
imgui.text("ABCDEFGHIJKLMNOPQRTSUVWXYZ\nabcdefghijklmnopqrstuvwxyz\n1234567890!@#$%^&*()")
imgui.image(imgui.get_io().fonts.texture_id, 512, 512)
imgui.end()
if __name__ == "__main__":
imgui.create_context()
window = glfw_init()
impl = GlfwRenderer(window)
start()
while not glfw.window_should_close(window):
glfw.poll_events()
impl.process_inputs()
imgui.new_frame()
gl.glClearColor(0.0, 0.0, 0.0, 1.0)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
onupdate()
imgui.render()
impl.render(imgui.get_draw_data())
glfw.swap_buffers(window)
impl.shutdown()
glfw.terminate()