Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Kivy Cookbook

You're reading from   Kivy Cookbook Enhance your skills in developing multi-touch applications with Kivy

Arrow left icon
Product type Paperback
Published in Aug 2015
Publisher
ISBN-13 9781783987382
Length 246 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Hugo Solis Hugo Solis
Author Profile Icon Hugo Solis
Hugo Solis
Hugo Solis Hugo Solis
Author Profile Icon Hugo Solis
Hugo Solis
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Kivy and the Kv Language FREE CHAPTER 2. Input, Motion, and Touch 3. Events 4. Widgets 5. Graphics – Canvas and Instructions 6. Advanced Graphics – Shaders and Rendering 7. The API in Detail 8. Packaging our Apps for PC 9. Kivy for Mobile Devices Index

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:

  1. Open a new file in gedit and save it as e4.py.
  2. Make a rule for the label.
  3. Provide the text for the label:
    <Label>:
        text: 'Hello World' 
  4. Open a new file in gedit and save it as e4.py.
  5. Import the Kivy framework.
  6. Provide a subclass to the App class.
  7. Implement its build() method, so it returns a widget instance.
  8. 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.

You have been reading a chapter from
Kivy Cookbook
Published in: Aug 2015
Publisher:
ISBN-13: 9781783987382
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image