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

Building your interfaces

Now we are going to create an interface, a very simple one, providing some basics in the developing of Kivy interfaces.

Getting ready

We are going to start to develop our first application with Kivy, and we need an editor for coding; you can use your favorite for Python without any problem. Here, we are using gedit, just because it comes with almost all GNU/Linux distros.

How to do it…

These steps will generate our first Kivy interface:

  1. Open a new file in gedit and save it as e1.py.
  2. Import the Kivy framework.
  3. Subclass the App class.
  4. Implement its build() method, so it returns a widget instance (the root of your widget tree).
  5. Instantiate this class and call its run() method.

The code for this is as follows:

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 MyApp(App):
    def build(self): 
        return Label(text='Hello world') 

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

How it works…

Well, let's see the code in detail; the first line is:

import kivy

This does the magic of linking Python with Kivy. The second line is optional:

kivy.require('1.9.0')

However, it is extremely useful because it prevents version conflicts; if you have an old Kivy version, you will have to update to at least the version that is required. In the third line:

from kivy.app import App

It is required that the base class of your app inherits from the App class, which is present in the kivy_installation_dir/kivy/app.py folder. This is to connect your app with the Kivy GUI. The fourth line is:

from kivy.uix.label import Label

The uix module is the section that holds the user interface elements, such as layouts and widgets. This is a very common import that you will use in the future. Moving on to the fifth line:

class MyApp(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 MyApp in this line. The sixth line is:

def build(self):

This is the function where you should initialize and return your root widget. This is what we do on the seventh line:

return Label(text='Hello world')

Here we initialize a Label with the text Hello World and return its instance. This label is the root widget of this app.

Now, on to the portion that makes our app come to life is in the eighth and ninth lines:

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

Here, the class MyApp is initialized, and its run() method is called. This initializes and starts our Kivy app.

There's more…

Something to point out is the name of the window, it will be called My because of the name of the base class. Ergo, if you want the title of the window be Win1, your base class must be Win1App. So, the fifth line would be:

class Win1App(App):

You must change the last one to:

Win1App().run()

Actually, a suffix of App isn't necessary; it is just commonly used in Kivy. If you want to include an App suffix in the title or spaces, override the title attribute as:

class Win1App(App):
    def build(self): 
        self.title = 'Win 1 App'

See also

If you want to run your interface, take a look at our recipe Running your code. Also you can take a look in the file kivy_installation_dir/kivy/app.py, which is a very good document to go deeper into Kivy apps.

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