Chapter 1. GUI Basics – Building an Interface
Kivy is a free, open source Python library that allows for quick and easy development of highly interactive multiplatform applications. Kivy's execution speed is comparable to the native mobile alternative, Java for Android or Objective C for iOS. Moreover, Kivy has the huge advantage of being able to run on multiple platforms, just as HTML5 does; in which case, Kivy performs better because it doesn't rely on a heavy browser, and many of its components are implemented in C using the Cython library in such a way that most of the graphics processing runs directly in the GPU. Kivy strikes a great balance between performance and portability across various hardware and software environments. Kivy emerges with a simple but ambitious goal in mind:
"… same code for every platform, at least what we use every day: Linux/Windows/Mac OS X/Android/iOS"
Mathieu Virbel (http://txzone.net/2011/01/kivy-next-pymt-on-android-step-1-done/)
This support has being extended to Raspberry Pi, thanks to a crowd funding campaign started by Mathieu Virbel, the creator of Kivy. Kivy was introduced for the first time at EuroPython 2011 as a Python framework designed for creating natural user interfaces. Since then, it has grown bigger and attracted an enthusiastic community.
This book requires some knowledge of Python, and very basic terminal skills, but also it requires some understanding of Object-Oriented Programming (OOP) concepts. In particular, it is assumed that you understand the concept of inheritance and the difference between instances and classes. Refer to the following table to review some of these concepts:
Concept |
URL |
---|---|
OOP | |
Inheritance |
http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) |
Instance | |
Class |
Before we start, you will need to install Kivy. The installation process for all different platforms is documented and regularly updated on the Kivy website: http://kivy.org/docs/installation/installation.html.
Note
All code in this book has been tested with Kivy 1.9.0 and both Python 2.7 and Python 3.4 (but 3.3 should work fine as well).
Note that packaging support for mobile is not yet complete for Python 3.3+. For now, if we want to create mobile apps for Android or iOS, we should use Python 2.7. If you want to know your Python version, you can execute python -V
in a terminal to check your installed Python version.
In this chapter, we start by creating user interfaces using one of Kivy's most fun and powerful components – the Kivy language (.kv
). The Kivy Language separates logic from presentation in order to keep an easy and intuitive code; it also links components at an interface level. In future chapters, you will also learn how to build and modify interfaces dynamically using pure Python code and Kivy as a library.
Here is a list of all the skills that you are about to learn:
- Launching a Kivy application
- Using the Kivy language
- Instantiating and personalizing widgets (GUI components) through basic properties and variables
- Differentiating between fixed, proportional, absolute, and relative coordinates
- Creating responsive GUIs through layouts
- Modularizing code in different files
This chapter covers all the basics for building a Graphical User Interface (GUI) in Kivy. First, we will learn techniques to run an application and how to use and integrate widgets. After that, we will introduce the main project of the book, the Comic Creator, and program the main structure of the GUI that we will continue using in the following two chapters. At the end of this chapter, you will be able to build a GUI starting from a pencil and paper sketch, and also learn some techniques to make the GUI responsive to the size of the window.