Python Kivy Application Development

The Kivy .kv Language




While you can get by writing Kivy applications without ever writing Kivy language (.kv), using the Kivy language may be useful to you for organization and separating application logic from the presentation of it.

It may also boil down to preference in the end. Try both and see what you like best.

Here, we're going to show a very basic implementation of the Kivy language.

KivyVideo3.py file:

from kivy.app import App
#kivy.require("1.8.0")
from kivy.uix.label import Label


class SimpleKivy(App):
    def build(self):
        return Label()

if __name__ == "__main__":
    SimpleKivy().run()
		

This code should be easy enough to follow if you've been following along with the series. Nothing new here.

So what gives? How does this .kv stuff work?

The naming convention for the .kv file needs to follow the main app class, so SimpleKivy should be the name of our .kv file.

So now, let's make the kivy file, calling it SimpleKivy.kv:

#:kivy 1.8.0

<Label>
    text: 'Hello'+' World!'
		

So this is our Kivy Language. The tag signs just note that this is the parent, then we have children under it. Here, we're just saying we want to have a label that says "Hello" + "World!"

You could also say text: "Hello world!" I chose to show that you can include some basic Python code as well within the Kivy files.

This is just a basic example, showing the same result as the previous tutorial. The main advantage by using the Kivy language is just for organization. If you have, say, 50 labels in total, 20 buttons, and various other widgets, it will clutter your Python script quite a bit to do all of the styling within it. It might just help for clarity alone to separate the two. Kivy language reminds me a lot of CSS, or stylesheets, for HTML. While you could write all of your styles in line with your HTML, it just makes more sense to separate it.


There exists 2 quiz/question(s) for this tutorial. for access to these, video downloads, and no ads.

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