Kivy Cookbook - Hugo Solis - E-Book

Kivy Cookbook E-Book

Hugo Solis

0,0
39,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 241

Veröffentlichungsjahr: 2015

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Kivy Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Kivy and the Kv Language
Introduction
Installing Kivy
Getting ready
How to do it…
How it works…
There's more…
Mac OS X
Microsoft Windows
See also
Building your interfaces
Getting ready
How to do it…
How it works…
There's more…
See also
Declaring properties within a class
Getting ready
How to do it…
How it works…
There's more…
See also
Relating Python code and the Kv language
Getting ready
How to do it…
How it works…
There's more…
See also
Referencing widgets
Getting ready
How to do it…
How it works…
There's more…
See also
Accessing widgets defined inside the Kv language in your Python code
Getting ready
How to do it…
How it works…
See also
Reusing styles in multiple widgets
Getting ready
How to do it…
How it works…
There's more…
See also
Designing with the Kv language
Getting ready
How to do it…
How it works…
There's more…
See also
Running your code
Getting ready
How to do it…
How it works…
There's more…
Mac OS X
Microsoft Windows
See also
Using Kivy garden
Getting ready
How to do it…
There's more…
Packing
2. Input, Motion, and Touch
Introduction
Using the mouse
Getting ready
How to do it…
How it works…
There's more…
See also
Evolving to the touchscreen
Getting ready
How to do it…
How it works…
There's more…
See also
Working with the accelerometer
Getting ready
How to do it…
How it works…
There's more…
See also
Using the gyroscope
Getting ready
How to do it…
How it works…
See also
The differences between the touch and motion events
Getting ready
How to do it…
How it works…
There's more…
See also
Recognizing touch shapes
Getting ready
How to do it…
How it works…
See also
Detecting multitapping
Getting ready
How to do it…
How it works…
Grabbing touch events
Getting ready
How to do it…
How it works…
Recording gestures
Getting ready
How to do it…
How it works…
There's more…
See also
3. Events
Introduction
Scheduling a one-time event
Getting ready
How to do it…
How it works…
There's more…
See also
Scheduling a repetitive event
Getting ready
How to do it…
How it works…
There's more…
See also
Triggering events
Getting ready
How to do it…
How it works…
There's more…
See also
Defining widget events
Getting ready
How to do it…
How it works…
There's more…
See also
Creating custom events
Getting ready
How to do it…
How it works…
There's more…
See also
Attaching callbacks
Getting ready
How to do it…
How it works…
See also
Declaring a property
Getting ready
How to do it…
How it works…
There's more…
See also
Compounding properties
Getting ready
How to do it…
How it works…
There's more…
See also
4. Widgets
Introduction
Using the basics: buttons, labels, and text inputs
Getting ready
How to do it…
How it works…
There's more…
See also
Manipulating the widget tree
Getting ready
How to do it…
How it works…
There's more…
See also
Traversing the tree
Getting ready
How to do it…
How it works…
Using swappable widgets
Getting ready
How to do it…
How it works…
See also
Organizing with layouts
Getting ready
How to do it…
How it works…
There's more…
Using FloatLayout
Getting ready
How to do it…
How it works…
There's more…
See also
Using BoxLayout
Getting ready
How to do it…
How it works…
There's more…
Using GridLayout
How to do it…
How it works…
There's more…
Using StackLayout
Getting ready
How to do it…
How it works…
There's more…
Using RelativeLayout
Getting ready
How to do it…
How it works…
Using AnchorLayout
How to do it…
How it works…
Working with ActionBar
Getting ready
How to do it…
How it works…
See also
5. Graphics – Canvas and Instructions
Introduction
Separating with the screen manager
Getting ready
How to do it…
How it works…
There's more…
Using drawing instructions
Getting ready
How to do it…
How it works…
There's more…
Using context instructions
Getting ready
How to do it…
How it works…
There's more…
Working with manipulating instructions
Getting ready
How to do it…
How it works…
There's more…
Rotating, translating, and scaling the canvas
Getting ready
How to do it…
How it works…
There's more…
Modifying with multitouching
Getting ready
How to do it…
How it works…
There's more…
Storing and retrieving the coordinate space context
Getting ready
How to do it…
Introducing animations
Getting ready
How to do it…
How it works…
There's more…
See also
6. Advanced Graphics – Shaders and Rendering
Introduction
Using Carousel
Getting ready
How to do it…
How it works…
There's more…
Creating and using Atlas
Getting ready
How to do it…
How it works…
Creating layouts
Getting ready
How to do it…
How it works…
Editing shaders
Getting ready
How to do it…
How it works…
There's more…
Creating widgets
Getting ready
How to do it…
How it works…
See also
Creating your own shader
Getting ready
How to do it…
How it works…
There's more…
See also
Rendering in a Framebuffer
Getting ready
How to do it…
How it works…
Optimizing graphics
Getting ready
How to do it…
There's more…
See also
7. The API in Detail
Introduction
Getting to know the API
Getting ready
How to do it…
How it works…
There's more…
See also
Using the asynchronous data loader
Getting ready
How to do it…
How it works…
There's more…
Logging objects
How to do it…
How it works…
There's more…
See also
Parsing
How to do it…
How it works…
See also
Applying utils
How to do it…
How it works…
See also
Leveraging the factory object
Getting ready
How to do it…
How it works…
There's more…
See also
Working with audio
Getting ready
How to do it…
How it works…
There's more…
See also
Working with video
Getting ready
How to do it…
How it works…
There's more…
See also
Working with a camera
Getting ready
How to do it…
There's more…
Using spelling
Getting ready
How to do it…
How it works…
Adding effects
Getting ready
How to do it…
How it works…
There's more…
See also
Advanced text manipulation
How to do it…
How it works…
There's more…
See also
8. Packaging our Apps for PC
Introduction
Packaging for Windows
Getting ready
How to do it…
How it works…
There's more…
See also
Including multimedia for Windows
Getting ready
How to do it…
How it works…
There's more…
See also
Running apps in Windows
Getting ready
How to do it…
How it works…
See also
Packaging for Mac OS
Getting ready
How to do it…
How it works…
There's more…
See also
Including multimedia for Mac OS
Getting ready
How to do it…
How it works…
There's more…
Running apps in Mac OS
Getting ready
How to do it…
How it works…
See also
Packaging for Linux
Getting ready
How to do it…
Including multimedia for Linux
Getting ready
How to do it…
Running apps in Linux
Getting ready
How to do it…
How it works…
9. Kivy for Mobile Devices
Introduction
Packaging for iOS
Getting ready
How to do it…
How it works…
There's more…
See also
Resizing the screen into iOS
Getting ready
How to do it…
How it works…
There's more…
See also
Leveraging mobile features in iOS
Getting ready
How to do it…
How it works…
There's more…
See also
Preparing for the App Store
Getting ready
How to do it…
How it works…
See also
Packaging for Android
Getting ready
How to do it…
How it works…
See also
Resizing the screen into Android
Getting ready
How to do it…
How it works…
There's more…
See also
Leveraging mobile features in Android
Getting ready
How to do it…
How it works…
There's more…
See also
Index

Kivy Cookbook

Kivy Cookbook

Copyright © 2015 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: August 2015

Production reference: 1170815

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78398-738-2

www.packtpub.com

Credits

Author

Hugo Solis

Reviewers

Davide Depau

Abhinav Jangda

Matt Lebrun

Patrick Louis

Commissioning Editor

Usha Iyer

Acquisition Editor

Richard Gall

Content Development Editor

Mamata Walkar

Technical Editor

Tejaswita Karvir

Copy Editor

Dipti Mankame

Project Coordinator

Shipra Chawhan

Proofreader

Safis Editing

Indexer

Priya Sane

Graphics

Sheetal Aute

Production Coordinator

Shantanu N. Zagade

Cover Work

Shantanu N. Zagade

About the Author

Hugo Solis is an assistant professor in the physics department at the University of Costa Rica. In the same institution, he collaborates with CITIC and CICIMA. His current research interests include computational cosmology, complexity, and the influence of hydrogen on material properties. He has wide experience in languages, including C/C++ and Python for scientific programming and visualization. He is a member of the Free Software Foundation and has contributed code to some free software projects. He has also been a technical reviewer for Mastering Object-oriented Python, Kivy: Interactive Applications in Python, and Learning Object-Oriented Programming by Packt Publishing. Currently, he is in charge of the IFT, a Costa Rican scientific nonprofit organization for the multidisciplinary practice of physics (http://iftucr.org).

I would like to thank God for this life and all his blessings. One of those blessings is my beloved mother, Katty Sanchez, whom I have to thank for her support and vanguard thoughts.

I am grateful to my amazing friends Laura Rojas, Jose David Cojal, Gerardo Lacy, Allan Lacy, Pamela Saborio, and Ana Segura for sharing this long ride in the life with me.

A special thanks to my professors, colleagues, and friends—Manuel Ortega, Daniel Azofeifa, Neville Clark, William Vargas, Max Chaves, and Gabriela Barrantes—and MICITT and CONICIT for supporting my PhD studies.

I would also like to thank my brothers, Harry and Geancarlo, for enduring a geek brother, and Helena Oses for her endearment and unique words.

I owe the most to all the people behind Kivy, Python, and Packt Publishing. Thank you.

About the Reviewers

Abhinav Jangda has a deep interest in the field of application development. He is an open source enthusiast. He has contributed to open source projects, including Kivy and GNOME. He was one of the main contributors to the development of Kivy designer. He has been using Kivy as his primary application development framework. Besides Python, he has developed applications in C, C++, Java, and C# using some framework, such as GTK+, Qt, Java SWT, and Windows Presentation Foundation. He has been researching in the fields of operating systems, compilers, and virtual machines. He loves working with Linux and developing applications for it.

Matt Lebrun is a software engineer from the Philippines. He recalls his first dibs in programming using QBasic when he was 13, and since then, he's only gone deeper into this sleep-depriving yet overly rewarding field.

After college education, he started his career customizing in-house and commercial ERP systems. Then, he moved on to telecoms support systems, where he mainly dabbled with C++, Perl, and Shell scripts.

Nowadays, he's making up for lost times with his passion for hacking on data-oriented and web-related technologies through Python and Django in Save22 Inc.

On his days off, you'll catch him volunteering for the local Python community, hacking on a side project with his girlfriend, or in hermit mode learning something new.

I would like to thank my girlfriend, Mickey, for supporting me in writing this book review.

Patrick Louis is an advocate of the learn by/from curiosity movement. For him, programming is just another way of expression—an art. He likes to contribute his free time to the open source projects that catch his attention. He's a core member of the nixers.net community.

www.PacktPub.com

Support files, eBooks, discount offers, and more

For support files and downloads related to your book, please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

Why Subscribe?

Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

Free Access for Packt account holders

If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.

Preface

The book is full of practical task-based recipes to be performed while developing multitouch applications with Kivy. The focus of this book is to guide you through the Kivy framework to develop apps and get your apps ready for distribution in App Stores and Android devices. We'll focus on common, real-world scenarios. You'll be able to leverage these recipes right away, which allows you to create most diverse apps and learn how to distribute them using the Kivy framework, and widen your proficiency in developing multitouch applications with Kivy.

What this book covers

Chapter 1, Kivy and the Kv Language, introduces the Python code, Kv language, widgets, and Kivy garden. The topics covered in this chapter lay the foundation for the remaining code samples in each chapter.

Chapter 2, Input, Motion, and Touch, demonstrates some of the most common recipient-related management tasks, such as evolving to the touchscreen, using the gyroscope, and detecting multitapping. You'll also learn how to recognize touch shapes, record gestures, and differentiate between touch and motion events.

Chapter 3, Events, shows how to schedule a one-time event and a repetitive event. Triggering events, defining widget events, creating custom events are also covered in this chapter.

Chapter 4, Widgets, covers the manipulation of widget tree, traversing of tree. It also covers the FloatLayout, BoxLayout, StackLayout, AnchorLayout, and action bar.

Chapter 5, Graphics – Canvas and Instructions, explains various methods, such as separating with the screen manager, using context instructions, and using drawing instructions. You'll learn how to modify with multitouching and storing and retrieving the coordinate space context.

Chapter 6, Advancing Graphics – Shaders and Rendering, covers Carousel, layouts, and shaders. The other topics covered in this chapter are rendering in a Framebuffer and optimizing graphics.

Chapter 7, The API in Detail, introduces API and covers the logging objects, parsing, applying utils, using spelling, adding effects, and adding text.

Chapter 8, Packaging our Apps for PC, shows how to perform packing for Windows, Linux, and MacOs. It also shows multimedia for Windows and running apps in Windows and MacOS.

Chapter 9, Kivy for Mobile Devices, explains various methods, such as packaging for iOS and resizing the screen into iOS. You'll learn preparing for the App Store, packaging for Android, and resizing the screen into Android.

What you need for this book

To complete the recipes in this book, you'll need the following:

Kivy 1.9.0Cross-platformWindows/OS X/LinuxiOS device / Android deviceiOS and OS X / Android and Linux

Who this book is for

This book is intended for developers who want to use features of the Kivy framework and develop multitouch applications. Prior experience with Kivy is not required although familiarity with Python is expected.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We are using again our KV file with a simple button and empty label with ID button1 and label1 respectively"

A block of code is set as follows:

<MyW>: Button: id: button1 text: 'Hello' on_press: root.animate(button1)

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

canvas.after: PopMatrix

Any command-line input or output is written as follows:

$ Kivy pyinstaller.py --windowed --name e1 /Users/Me/Documents/e1app/e1.py

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "We are going to use one simple Python files that will just show our Hello World text"

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.

Chapter 1. Kivy and the Kv Language

In this first chapter, we will cover the following recipes:

Installing KivyBuilding your interfacesDeclaring properties within a classRelating the Python code and the Kv languageReferencing widgetsAccessing widgets defined inside the Kv language in your Python codeReusing styles in multiple widgetsDesigning with the Kv languageRunning your codeUsing Kivy garden

Introduction

The first chapter is going to introduce the reader to the Kivy framework, its basis, and the Kv language. This is necessary work and a common base for the next chapters. If this is your first time using Kivy, it is advised that you do not skip this chapter. However, if you do, remember to return to this chapter if you need to install a supporting tool or verify any concept that you need to support your current solution.

Installing Kivy

This recipe will teach you how to install Kivy on a personal computer, which is the first step in starting to develop great software.

Getting ready

We will assume that you already have GNU/Linux (preferably Ubuntu/Debian/Trisquel, we recommend the last one) and Python installed on it. Usually, Python is already installed on the aforementioned GNU/Linux distributions. We will also assume that you are using Python version 2.7 or higher.

How to do it…

Add one of the Personal Package Archives (PPAs) that you prefer; our recommendation is the following stable one:
stable builds: $ sudo add-apt-repository ppa:kivy- team/kivynightly builds: $ sudo add-apt-repository ppa:kivy- team/kivy-daily
Update your package list using your package manager:
$ sudo apt-get update
Install Python-kivy and, optionally, the examples that are found in Python-kivy-examples:
$ sudo apt-get install Python-kivy
Verify the installation. Call Python from the console and execute this command:
import kivy

How it works…

There are many ways to get Kivy installed on your computer. Here we are describing probably the easiest way using your distribution's package manager. In the first step, we are adding a PPA as an APT repository to provide you with two different options: the stable one, for which all the Kivy products have been well tested, and the nightly one, which are packages under active development. Actually, for Ubuntu, you can skip the first step; it was just to get the latest version of Kivy.

In the second step, we update the list of available packages to include the Kivy repository. The third step is where the installation of Kivy really happens by using the distribution's package manager. In the last step, we verify if Kivy is working with the command that imports Kivy. If everything is OK we will see the following:

[INFO ] Kivy v1.9.0

It shows the Kivy version that you installed in your system, which is v1.9.0 in this case. Remember to exit Python, for which we use the command quit().

There's more…

Now we will say something about Mac OS X and Microsoft Windows; for them, Kivy provides what is called portable packages. For an easy way to get Kivy running, just go to http://kivy.org/#download.

Mac OS X

Download the dmg file, double-click to open it, and drag the Kivy.app into your Applications folder. Ready!

You should run the make-symlinks script to make Kivy available in the shell system. Thus, you can run Kivy from the terminal.

Microsoft Windows

Download the .zip file and unzip it. There is a file named kivy.bat that must be copied as a shortcut into your SendTo folder.

See also

Well, if you are using a different operating system, you are always able to go to http://kivy.org/#download and look for the one that you are using. Also, if you want to build Kivy from the source code, refer to Chapter 8, Packaging our Apps for PC the recipe Packing for Linux.