KV Language

Date: February 24th 2016
Last updated: February 24th 2016

The kv language is a declarative language that describes the layout, interface and interactions.

The kv language is typically added to a kv file that is saved in the same directory as main.py. The name of the kv file is conventionally determined by the name of your app (e.g. MyApp has a my.kv file, TestApp has a test.kv file and RaysApp has a ray.kv file)

The language is much like CSS. After kivy instantiates a subclass of App, it looks for a kv file. Note that the "#:kivy 1.9.0" line in my.kv is read by kivy as a header. You get an error if your version is older than the one specified here.

basicapp.py

import kivy
kivy.require('1.9.0')
from kivy.app import App

class MyApp(App):
    pass

if __name__ == '__main__':
    MyApp().run()

my.kv
Some things to note: (1) "#:kivy" is special syntax to define a header, (2) "Button:" is a root widget. A root widget has no indentation followed by a colon (:). It is set at the root level of this app.

#:kivy 1.9.0
Button:
    text: 'Hello this is Ray from my.kv'

Output
The app has a single button containing our text. Note in this screenshot the button has been clicked to appear blue (Here I have clicked the right mouse button to activate the kivy button. This added a red dot (explained later) and held the button down. The button does nothing.
basic kivy app using kv file

Useful resources

results matching ""

    No results matching ""