Relating Python code and the Kv language
This recipe will teach you how to relate the Kv language to Python code. Kivy provides a design language specifically geared toward easy and scalable GUI design. The Kv language separates the interface design from the application logic, adhering to the separation of concerns principle, where the application remains in Python and the design remains in the Kv language.
Getting ready
This recipe will create the same interface as the recipe Building your interfaces, but now using the Kv language; hence, it could be educational to look the code in there to make some comparisons. Again we are using gedit, just because it comes with almost all GNU/Linux distros.
How to do it…
These steps will generate our Kivy interface using the Kv language:
- Open a new file in gedit and save it as
e4.py
. - Make a rule for the label.
- Provide the text for the label:
<Label>: text: 'Hello World'
- Open a new file in gedit and save it as
e4.py
. - Import the Kivy framework.
- Provide a subclass to the
App
class. - Implement its
build()
method, so it returns a widget instance. - Instantiate this class and call its
run()
method:import kivy kivy.require('1.9.0') # Kivy ver where the code has been tested! from kivy.app import App from kivy.uix.label import Label class e4App(App): def build(self): return Label() if __name__ == '__main__': e4App().run()
How it works…
Well, let's see the code in detail. The first line for the file e4.kv
is:
<Label>:
This line creates the rule for a Label
. The second one is:
text: 'Hello World'
In this, we define the text property for the Label
with the value 'Hello World'
.
Now, the first four lines of the Python file are the common ones to use Kivy in Python, and we already reviewed them in this chapter recipe Building your Interfaces. Moving on to the fifth line:
class e4App(App):
This is where we define the base class of our Kivy app. You should only ever need to change the name of your app e4App
in this line, and here is where the relationship between the Kv language and the Python code occurs. What happens is that Kivy looks for a file named e4.kv
, which could be present or not. The sixth line is:
def build(self):
This is the function where you initialize and return your root widget. This is what we do on the seventh line:
return Label()
Here we initialize a Label
and returned its instance. This Label
is the root widget of this app, and it must be the same in the KV file.
Now, on to the portion that makes our app come to life is in the eighth and ninth lines:
if __name__ == '__main__': e4App().run()
Here, the class e4App
is initialized, and its run()
method is called. This initializes and starts our Kivy app.
There's more…
The filename of the KV file should be such that adding the name of the KV file and App
will become the name of the subclass of the App
class. For example, if you change the name to Win1App
, then you should also change the KV filename to Win1.kv
.
Something to point out is that we can incorporate the KV code inside the Python file with the class Builder
. We just add a few lines between the fourth and fifth lines in the Python code to import the Builder package and the override of the method as follows:
from kivy.lang import Builder Builder.load_string(''' <Label>: text: 'Hello World' ''')
See also
If you want to run your interface, take a look at our recipe Running your Code and compare the result with the recipe Building your Interfaces.