Andi Hasan
Andi Hasan

Reputation: 1

Traceback (most recent call last) OSerror and font not found

enter image description herei will run python script with kivy and kivymd but this problem appears, how to solve it? I already installed the kivy library and kivymd still looks like this...

from kivy.app import App from kivy.uix.button import Button

class TestApp(App): def build(self): return Button(text='Hello World')

TestApp().run()

Traceback (most last): File "d:/Python/main python/main.py", line 2, in from kivy.uix.label import Label File "C:\Users\andihasan AppData\Local\Programs\ Python\Python38-32\lib\site-packages\kivy\uix\labe 1.py", line 286, in from kivy.core.text import Label as Corelabel, DEFAULT_FONT File "C:\Users\andihasan AppData \Local\Programs \Python\Python38-32\lib\site-packages\kivy\core\tex t_init_-py", line 1013, in Label.register(DEFAULT_FONT, *default_font_paths) File "C: \Users\ andihasanAppData\Local\Programs \Python\Python38-32\lib\site-packages\kivy\coreltex t_init_-py", line 315, in register raise IOError('File {e} not found'.format(font_type)) OSError: File data/fonts/Roboto-Regular.ttf not found

Upvotes: 0

Views: 719

Answers (1)

darkmatter08
darkmatter08

Reputation: 136

OSError: File data/fonts/Roboto-Regular.ttf not found.

This line is the cause of your error. if you're trying to use custom fonts in your kivymd project, you need to have the font file. that is the .ttf file and to use the font without having to put the full directory, you need to register the font with the following lines.

first import label base

from kivy.core.text import LabelBase

then register the font with an easier name to refer to.

if __name__ ==  "__main__":
    LabelBase.register(name="RobotoReg", fn_regular="{full_path_to}/Roboto-Regular.ttf")

you can then use it in your .kv file like so:

MDLabel:
    text: "Text to test if font name works"
    size_hint_x: 1
    pos_hint: {"center_x":.5, "center_y": .5}
    font_name: "RobotoReg"
    font_style: "Caption"
    halign: "center"

Upvotes: 0

Related Questions