'How to add new fonts to kivy?
My code in python file is:
from kivy.app import App
from kivy.core.text import LabelBase
class ClockApp(App):
pass
if __name__ == '__main__':
LabelBase.register(name='Roboto',
fn_regular='Roboto-Thin.ttf',
fn_bold='Roboto-Medium.ttf')
ClockApp().run()
and in kivy file:
BoxLayout:
orientation: 'vertical'
Label:
text: '00:00:00'
font_name: 'Roboto'
font_size: 60
But running the program raises the followig error:
File "/lib/python3.10/site-packages/kivy/core/text/__init__.py", line 315, in register
raise IOError('File {0} not found'.format(font_type))
OSError: File Roboto-Thin.ttf not found
I Download all Roboto font family members in .ttf format and save them in ~/.fonts folder. But still error raises.
Solution 1:[1]
Copying the fonts to the ~./fonts folder will not allow them to be recognized by kivy. You need to manually add them to kivy using their full path. I would suggest to keep your fonts with your project files as it will be easier to maintain the structure of your code.
For example:
LabelBase.register(name='Roboto',
fn_regular='/home/guhan/Documents/project/Roboto-Thin.ttf')
Here I have used my own path full path to the font.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Guhan SenSam |
