(Ebook) Kivy - Interactive Applications and Games in Python, 2nd Edition: Create responsive cross-platform UI/UX applications and games in Python using the open source Kivy library by Roberto Ulloa ISBN 9781785286926, 1785286927 pdf download
(Ebook) Kivy - Interactive Applications and Games in Python, 2nd Edition: Create responsive cross-platform UI/UX applications and games in Python using the open source Kivy library by Roberto Ulloa ISBN 9781785286926, 1785286927 pdf download
https://ebooknice.com/product/godot-4-game-development-projects-build-five-
cross-platform-2d-and-3d-games-using-one-of-the-most-powerful-open-source-
game-engines-56267534
https://ebooknice.com/product/building-android-apps-in-python-using-kivy-
with-android-studio-with-pyjnius-plyer-and-buildozer-50195834
https://ebooknice.com/product/introducing-net-maui-build-and-deploy-cross-
platform-applications-using-c-and-net-multi-platform-app-ui-48399910
(Ebook) Mastering Qt 5: Create stunning cross-platform
applications using C++ with Qt Widgets and QML with Qt Quick,
2nd Edition by Guillaume Lazar & Robin Penea ISBN B07DH9YK9Q
https://ebooknice.com/product/mastering-qt-5-create-stunning-cross-
platform-applications-using-c-with-qt-widgets-and-qml-with-qt-quick-2nd-
edition-55497936
https://ebooknice.com/product/kivy-cookbook-43275852
https://ebooknice.com/product/create-gui-applications-with-python-
qt5-pyside2-edition-11547142
https://ebooknice.com/product/pro-silverlight-4-in-vb-3rd-edition-create-
cross-platform-net-applications-for-the-browser-5473536
www.it-ebooks.info
Kivy – Interactive Applications
and Games in Python
Second Edition
Roberto Ulloa
BIRMINGHAM - MUMBAI
www.it-ebooks.info
Kivy – Interactive Applications and Games in Python
Second Edition
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.
ISBN 978-1-78528-692-6
www.packtpub.com
www.it-ebooks.info
Credits
Reviewers Proofreader
Takumi Adachi Safis Editing
Philip Bjorge
Joe Dorocak Indexer
Priya Sane
Vijay Mahrra
Edward C. Delaporte V
Graphics
Sheetal Aute
Commissioning Editor
Disha Haria
Nadeem N. Bagban
Jason Monteiro
Acquisition Editor
Nikhil Karkal Production Coordinator
Nitesh Thakur
Technical Editor
Ankur Ghiye
Copy Editor
Adithi Shetty
www.it-ebooks.info
About the Author
www.it-ebooks.info
About the Reviewers
Philip Bjorge is a full-stack developer who has worked on projects for health,
amusement parks, academics, and high-tech industries. Prior to joining Substantial,
a Seattle-based software design agency, he worked on the Xbox Music and Video
team at Microsoft. Most notably, he was a developer for Surface Music Kit, an app
that was featured at the Surface 2 press conference unveiling and was on display in
Microsoft stores nationwide.
www.it-ebooks.info
Joe Dorocak, whose Internet moniker is Joe Codeswell, is a very experienced
programmer. He enjoys creating readable code that implements project requirements
efficiently and in a manner that can be easily understood. He considers writing code
akin to writing poetry. He crafts his code so it acts as communication, not only with
the machine platforms on which it runs, but also with the human programmers who
will read it in the future.
Joe has been employed directly and also in a contractual role by start-ups and by
many major top-shelf companies, including IBM, HP, and GTE/Sprint.
Joe has also worked on Kivy Blueprints by Mark Vasilko and Functional Programming
in JavaScript by Dan Mantyla.
www.it-ebooks.info
Vijay Mahrra is an experienced system administrator, developer, and programmer
with over 20 years of experience from the very early days of the Web to the present
day, contributing his knowledge and experience to various free and open source
projects along the way.
Edward C. Delaporte V has been creating and using software since the
mid 1980s.
www.it-ebooks.info
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.
TM
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 Packt
• Copy and paste, print, and bookmark content
• On demand and accessible via a web browser
www.it-ebooks.info
Table of Contents
Preface iii
Chapter 1: GUI Basics – Building an Interface 1
Basic interface – Hello World! 3
Basic widgets – labels and buttons 7
Layouts 10
Embedding layouts 16
PageLayout – swiping pages 19
Our project – Comic Creator 22
Summary 29
Chapter 2: Graphics – the Canvas 31
Understanding the canvas 32
Drawing basic shapes 33
Adding images, colors, and backgrounds 41
Structuring graphic instructions 42
Rotating, translating, and scaling the coordinate space 44
Comic Creator: PushMatrix and PopMatrix 47
Summary 53
Chapter 3: Widget Events – Binding Actions 55
Attributes, ID, and root 56
Basic widget events – dragging the stickman 58
Localizing coordinates – adding stickmen 65
Binding and unbinding events – sizing limbs and heads 69
Binding events in the Kivy language 74
Creating your own events – the magical properties 77
Kivy and its properties 80
Summary 84
[i]
www.it-ebooks.info
Table of Contents
[ ii ]
www.it-ebooks.info
Preface
Mobile devices have transformed the way applications are perceived. They
have increased in interaction types; the user now expects gestures, multi-touches,
animations, responsiveness, virtual keyboards, and magic-pens. Moreover,
compatibility has become a must if you want to avoid the barriers imposed by
major operating systems. Kivy is an open source Python solution that covers these
market needs with an easy-to-learn and rapid development approach. Kivy continues
to grow fast and two versions have been released since the first publication of this
book in September 2013. Thanks to an enthusiastic community, Kivy is making its way
in an extremely competitive territory in which it stands out for offering both a cross-
platform and efficient alternative to native development and HTML5.
This book introduces you to the Kivy world, covering a large variety of important
topics related to interactive applications and games development. The components
presented in this book were selected according to their usefulness for developing
state-of-art applications and also for serving as an example of broader Kivy
functionalities. Following this approach, the book covers a big part of the
Kivy library.
This book provides you with examples to understand their use and how to
integrate the three projects that come with this book. The first one, the comic
creator, exemplifies how to build a user interface (Chapter 1, GUI Basics – Building
an Interface), how to draw vector shapes in the screen (Chapter 2, Graphics – the
Canvas), how to bind user interactions with pieces codes (Chapter 3, Widget Events
– Binding Actions), and other components related to improving the user experience
(Chapter 4, Improving the User Experience). The second project, Invaders Revenge, is an
interactive game that introduces you to the use of animations, scheduling of tasks,
keyboard events, and multi-touch control (Chapter 5, Invaders Revenge – an Interactive
Multi-touch Game). The third project, Kivy Player, teaches how we can control video
streams with a modern design and responsive interactions to maximize the use of the
screen (Chapter 6, Kivy Player – a TED Video Streamer).
[ iii ]
www.it-ebooks.info
Preface
Occasionally, this book explains some technical but important Kivy concepts that are
related to the Kivy class structure and implementation, or the order and strategies to
draw on the screen. These explanations give the reader some insights into the Kivy
internals that will help them solve potential problems when they develop their own
projects. Even though they are not necessary for the comprehension of the main
topics of this book, they will become important lessons when the reader faces new
situations implementing their own applications.
This book grabs the reader's attention by stating interesting programming scenarios.
The sections are generally short and straightforward, making the learning process
constant. These short sections will also serve as a reference when the reader finishes
the book. However, serving as a reference doesn't prevent the text from achieving the
main goal, which is teaching bigger projects that connect the small topics. At the end
of this book, the reader will feel comfortable to start their own project.
Chapter 2, Graphics – the Canvas, explains the use of the canvas and how to draw
vector figures on the screen.
Chapter 3, Widget Events – Binding Actions, teaches how to connect the interactions of
the user through the interface with particular code inside the program.
[ iv ]
www.it-ebooks.info
Preface
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:
"This is the reason we included the on_touch_down event."
[v]
www.it-ebooks.info
Preface
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 need
an alternate way to stop the video (different from the Stop button)."
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.
[ vi ]
www.it-ebooks.info
Preface
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.
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.
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.
[ vii ]
www.it-ebooks.info
Preface
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.
[ viii ]
www.it-ebooks.info
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"
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.
[1]
www.it-ebooks.info
GUI Basics – Building an Interface
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 http://en.wikipedia.org/wiki/Object-oriented_
programming
Inheritance http://en.wikipedia.org/wiki/Inheritance_(object-
oriented_programming)
Instance http://en.wikipedia.org/wiki/Instance_(computer_
science)
Class http://en.wikipedia.org/wiki/Class_(computer_
science)
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.
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.
[2]
www.it-ebooks.info
Chapter 1
Here is a list of all the skills that you are about to learn:
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.
[3]
www.it-ebooks.info
Random documents with unrelated
content Scribd suggests to you:
The Project Gutenberg eBook of La fabrique de
mariages, Vol. 4
This ebook is for the use of anyone anywhere in the United States
and most other parts of the world at no cost and with almost no
restrictions whatsoever. You may copy it, give it away or re-use it
under the terms of the Project Gutenberg License included with this
ebook or online at www.gutenberg.org. If you are not located in the
United States, you will have to check the laws of the country where
you are located before using this eBook.
Language: French
LA
FABRIQUE DE MARIAGES.
COLLECTION HETZEL.
LA
FABRIQUE DE MARIAGES
PAR
PAUL FÉVAL.
IV
Édition autorisée pour la Belgique et l’Étranger,
interdite pour la France.
LEIPZIG,
ALPH. DURR, LIBRAIRE-ÉDITEUR.
—
1858
BRUXELLES.—TYP. DE J. VANBUGGENHOUDT,
Rue de Schaerbeek, 12.
DEUXIÈME PARTIE.
——
L’HOTEL DE MERSANZ
(SUITE).
XII
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebooknice.com