Aatu Tahkola
Aatu Tahkola

Reputation: 35

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?

Upvotes: 0

Views: 2912

Answers (2)

Aniket
Aniket

Reputation: 11

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

Upvotes: 1

Ne1zvestnyj
Ne1zvestnyj

Reputation: 1397

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

Upvotes: 1

Related Questions