Kivy showing a black screen when trying to use kv file?

Question:

So I want to do the styling of my Kivy app in a external .kv file, but when I run the main code, nothing appears to the black Kivy window.

Here’s my main file called main.py

import kivy
from kivy.app import App
from kivy.uix.widget import Widget

class MyGrid(Widget):
    pass
    
class MyApp(App):
    def build(self):
        return MyGrid()

if __name__ == '__main__':
    MyApp().run()

And here’s the .kv file located in the same directory and called my.kv

#:kivy 2.0.0
<MyGrid>:
    Label:
        text: "example example"

So I’m not getting any error, just nothing appearing in the Kivy GUI when I run the main code.

Why is this and how to fix it?

Asked By: Aatu Tahkola

||

Answers:

In order to load widgets from a separate kivy file, you need to import Builder:

from kivy.lang.builder import Builder
Builder.load_file('my.kv')

or in .py file

Builder.load_string("""
<MyGrid>:
    Label:
        text: "example example"
""")
Answered By: Neizvestnyj

The kivy file name should always be the App class name, in your case you should save file with MyApp.kv
else you need to use Builder to import

Answered By: Aniket