这个程序使用 Tkinter,我正在尝试使用我的 Windows 机器上没有安装的自定义字体来显示文本,因为其他用户肯定也不会安装这个字体..
我试过使用 pyglet 但它只在安装字体时有效.. 这是一个简单的例子:
import tkinter as tk, pyglet
pyglet.font.add_file('digital-7.ttf') #font file is in the same folder of this python script.
root = tk.Tk()
MyLabel = tk.Label(root, text="EXAMPLE TEXT", font=('Digital-7', 25)) #font family name is correct.
MyLabel.pack()
root.mainloop()
我试过在我的机器上安装和卸载相同的字体,结果只有在安装字体时才能实现..
有人知道我做错了什么吗?因为我没有收到任何错误所以我不知道该怎么做.. 我在 Windows 10 机器上使用 Python 3.11..
此外,如果有人知道无需安装即可使用下载字体的其他方法,即使必须使用 Kivy、PyQt 等,它也会对我有帮助。
提前谢谢大家..
从 2.0.0 版本开始,使用
pyglet.font.directwrite.Win32DirectWriteFont
类代替 pyglet.font.win32.GDIPlusFont
类,前者似乎不起作用。
要么使用 1.5.27 等旧版本,要么在导入后立即设置
GDIPlusFont
强制使用 pyglet.options['win32_gdi_font'] = True
pyglet
:
import tkinter as tk
import pyglet
pyglet.options['win32_gdi_font'] = True
pyglet.font.add_file('digital-7.ttf')
root = tk.Tk()
MyLabel = tk.Label(root, text="EXAMPLE TEXT", font=('Digital-7', 25)) #font family name is correct.
MyLabel.pack()
root.mainloop()
结果: