pythonuser-interfacekivykivy-language

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


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?


Solution

  • 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"
    """)