Search code examples
pythonkivykivy-language

How can i use .kv file in different folders?


I don't know how to use .kv file so i just want to summary example. For example let we have 2 folders.

These folders: src and design.

src folder contain: main.py
design folder contain: main.kv

I want to know just simple example in this situation. How can i access from main.py file to main.kv file. I researched but i didn't understand very well. Please just give me a simple example.


Solution

  • you can use the Builder object to load all of the .kv files you want.

       # useful for creating paths from multiple parts
       from pathlib import Path, PurePath
       #
       from kivy.lang import Builder
       # load_file can be called multiple times
       Builder.load_file(str(PurePath("c:/", "users", "public", "my_lib.kv")))
       Builder.load_file(str(PurePath("c:/", "users", "public", "my_wigdet.kv")))
    

    I don't know the reason but in at least one of my apps, I had to load the one .kv file for the main app in a different way.

    main_gui_app = App()  # substitute your own app creation..
    main_gui_app.kv_file = str(Path(*your_path, "main_app.kv"))
    main_qui_app.run()