Builder for loading .kv Files




This tutorial will be covering the Kivy builder, which is used for building the .kv information. Instead of using the somewhat strange naming conventions for the app / .kv file, you can explicitly dictate the .kv file, or even just the .kv code string without needing a file at all.

from kivy.app import App
from kivy.lang import Builder

presentation = Builder.load_file("main.kv")

class MainApp(App):
    
    def build(self):
        return presentation
		

if __name__ == "__main__":

    MainApp().run()
		

First, to use the builder, we need to import it.

Then, we use Builder.load_file() to load the file we want to use, explicitly. In our case, we're loading in "main.kv."

While we have the root app class still being called MainApp, we could call it Splat, for example, and things would still work just fine.

You can also use Builder.load_string(), and then you can use a multi-line string, writing all of your kv code right in the py file.

The next tutorial:





  • Kivy with Python tutorial for Mobile Application Development Part 1
  • Kivy Widgets and Labels
  • The Kivy .kv Language
  • Kivy .kv Language cont'd
  • Dynamic Resizable Placement
  • Layouts: Float Layout
  • Getting Mouse / Press / Touch Input
  • Simple Drawing Application
  • Builder for loading .kv Files
  • Screen Manager for Multiple Screens
  • Drawing Application with Screen Manager
  • Adding better Navigation