Chapter 1. GUI Basics – Building an Interface
Kivy emerges as a successor of PyMT (a library for multitouch applications) with a simple but ambitious goal in mind — same code for every commonplace platform: Linux / Windows / Mac OS X / Macosx / Android / iOS (Mathieu Virbel, http://txzone.net/2011/01/kivy-next-pymt-on-android-step-1-done/). This support is being extended to Raspberry Pi thanks to a founding campaign started by Mathieu Virbel, the creator of Kivy. Kivy was introduced in the EuroPython 2011, as a Python framework designed for creating natural user interfaces.
So, let's start creating user interfaces using one of its fun and powerful components, the Kivy language (.kv
). The Kivy language helps us to separate the logic from the presentation. This is a fundamental engineering concept that helps to keep an easy and intuitive code. Nonetheless, it is possible to build a Kivy application using pure Python and Kivy as a library. We will also learn those concepts in later chapters because they allow us to modify interfaces dynamically.
This chapter covers all the basics for building a graphical user interface (GUI) in Kivy. Afterwards, you will be able to build practically any GUI you have in your mind, and even make them responsive to the size of window! The following is a list of all the skills that you're about to learn:
Launching a Kivy application
The Kivy language
Creating and using widgets (GUI components)
Basic properties and variables of the widgets
Fixed, proportional, absolute, and relative coordinates
Organizing GUIs through layouts
Tips for achieving responsive GUIs
Apart from Python, this chapter requires some knowledge about Object-Oriented Programming (http://en.wikipedia.org/wiki/Object-oriented_programming) concepts. In particular, inheritance (http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)) and the difference between instances (http://en.wikipedia.org/wiki/Instance_(computer_science)) and classes (http://en.wikipedia.org/wiki/Class_(computer_science)) will be assumed. Before starting, you will need to install Kivy (The instructions can be found in http://kivy.org/docs/installation/installation.html). The book examples were tested on Kivy 1.7.0 but a more recent version should work as well.
At the end of this chapter, we will be able to build a GUI starting from a pencil and paper sketch. We will introduce the main project of the book — the Comic Creator, and implement the main structure of the GUI.