Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Kivy Cookbook
  • Table Of Contents Toc
  • Feedback & Rating feedback
Kivy Cookbook

Kivy Cookbook

By : Hugo Solis, Solis
2.5 (2)
close
close
Kivy Cookbook

Kivy Cookbook

2.5 (2)
By: Hugo Solis, Solis

Overview of this book

Kivy is an open-source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps. It is a promising Python framework to develop UI and UX apps in a cross-platform environment, under the Python philosophy. Kivy Cookbook is a practical book that will guide you through the Kivy framework to develop apps and get your apps ready for distribution in App Store and Android devices. You will start off with installing Kivy and building your interfaces. You will learn how to work the accelerometer and create custom events. Then, you will understand how to use the basics, buttons, labels and text inputs and manipulate the widget tree. Next, you will be able to work with manipulating instructions, create an atlas and layouts. Moving on, you will learn packing for Windows and packing for iOS, and use TestDrive. By the end of the book, you will have learnt in detail the relevant features and tools in Kivy and how to create portable packages to distribute your apps in the most used platforms.
Table of Contents (11 chapters)
close
close
10
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.

Create a Note

Modal Close icon
You need to login to use this feature.
notes
bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Delete Note

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Edit Note

Modal Close icon
Write a note (max 255 characters)
Cancel
Update Note

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY