How properly add classes from other files to kivy projects?

I’m learning kivy and projects on python bigger then 1 file.

How properly add new classes in new files?

Now i create
a) Example.py with definition and add import to main.py file
b) example.kv with KV code and add include to this files where i want use it this element.

If i don’t import/include both files project don’t see class but when i do it kivy says that it’s already included. So us in title? How do it properly?

warning

See my answer in this post: https://stackoverflow.com/a/77708993/10398943

kv code has a global namespace, it only needs to be loaded once.

By default kivy will try to load a kv file with the same name as the class derived from App. Make sure you are not importing it twice.

For your example, lets say we have:
main.py – the main python file
main.kv – the main kv file, defines the root widget
screen_1.py defines the methods used in class Screen1
screen_1.kv defines the kv for Screen1

# in main.py import Screen1
from screen_1 import Screen1
...
# in main.py load the main.kv file in build()

class MyFirstApp(App):
    def build(self):
        return Builder.loadfile('main.kv')

#------------------
# in screen_1.py load the kv file.
Builder.load_file('screen_1.kv') 

Leave a Comment