Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Kivy Blueprints
Kivy Blueprints

Kivy Blueprints: Build your very own app-store-ready, multi-touch games and applications with Kivy!

Arrow left icon
Profile Icon Vasilkov
Arrow right icon
₱579.99 ₱2000.99
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7 (10 Ratings)
eBook Jan 2015 282 pages 1st Edition
eBook
₱579.99 ₱2000.99
Paperback
₱2500.99
Subscription
Free Trial
Arrow left icon
Profile Icon Vasilkov
Arrow right icon
₱579.99 ₱2000.99
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7 (10 Ratings)
eBook Jan 2015 282 pages 1st Edition
eBook
₱579.99 ₱2000.99
Paperback
₱2500.99
Subscription
Free Trial
eBook
₱579.99 ₱2000.99
Paperback
₱2500.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Kivy Blueprints

Chapter 2. Building a Paint App

In Chapter 1, Building a Clock App, we built an application from Kivy's standard components: layouts, text labels, and buttons. We were able to significantly customize the look of these components while retaining a very high level of abstraction—working with full-fledged widgets, as opposed to individual graphical primitives. This is convenient for certain types of applications but not always desirable, and as you will see shortly, the Kivy framework also provides tools to work with a lower level of abstraction: draw points and lines.

I believe that the best way to play with free-form graphics is by building a painting app. Our application, when complete, will be somewhat similar to the MS Paint application that comes bundled with the Windows OS.

Unlike Microsoft Paint, our Kivy Paint app will be fully cross-platform, including mobile devices running Android and iOS. Also, we will deliberately omit many features found in "real&quot...

Setting the stage

Initially, the entire surface of our app is occupied by the root widget, in this case, that's the canvas the user can paint upon. We won't devote any screen space to the instruments' area until later.

As you probably know by now, the root widget is the outermost widget in the hierarchy. Every Kivy application has one, and it can be pretty much anything, depending on the desired behavior. As seen in Chapter 1, Building a Clock App, BoxLayout is a suitable root widget; it was sufficient as we had no additional requirements for it, and layouts are designed to work as containers for other controls.

In the case of a Paint app, we need its root widget to adhere to much more interesting requirements; the user should be able to draw lines, possibly utilizing multitouch functionality, if available. At the moment, Kivy has no built-in controls suitable for the task at hand, so we will need to create our own.

Building new Kivy widgets is simple. As soon as our class...

Fine-tuning the looks

First, let's tweak the appearance of our app. This isn't exactly a critical functionality, but bear with me here, as these customizations are commonly requested and also pretty easy to set up. I'll briefly describe the properties that we covered in the previous chapter, and we'll add a number of new tweaks, such as window size and change of the mouse cursor.

Visual appearance

I strongly believe that the background color of any Paint app should initially be white. You're probably already familiar with this setting from the first chapter. Here's the line of code we add after the __name__ == '__main__' line to achieve the desired effect:

from kivy.core.window import Window
from kivy.utils import get_color_from_hex

Window.clearcolor = get_color_from_hex('#FFFFFF')

You may want to put most of the import lines where they usually belong, near the beginning of a program file. As you will learn shortly, some imports in Kivy are...

Drawing touches

To illustrate one possible scenario for reacting to the touch input, let's draw a circle every time the user touches (or clicks) the screen.

A Widget has an on_touch_down event that will come in handy for this task. We're interested in just the coordinates of every touch for the time being, and they are accessible as follows:

class CanvasWidget(Widget):
    def on_touch_down(self, touch):
        print(touch.x, touch.y)

This example prints the position of touches as they occur. To draw something on the screen instead, we will use the Widget.canvas property. Kivy's Canvas is a logical drawable surface that abstracts away the underlying OpenGL renderer. Unlike the low-level graphical API, the canvas is stateful and preserves the drawing instructions that were added to it.

Speaking of drawing primitives, many of those can be imported from the kivy.graphics package. Examples of drawing instructions are Color, Line, Rectangle, and Bezier, among others.

A very short...

Clearing the screen

Right now, the only way to clear the screen in our little app is to restart it. Let's add a button for deleting everything from the canvas to our UI, which is very minimalistic at the moment. We'll reuse the button look from the previous app, so there will be nothing new about theming; the interesting part here is positioning.

In our first program, the Clock app from Chapter 1, Building a Clock App, we didn't work on any explicit positioning at all, as everything was being held in place by nested BoxLayouts. Now, however, we don't have any layout to our program as the root widget is our very own CanvasWidget, and we didn't implement any logic to position its children.

In Kivy, the absence of an explicit layout means that every widget has full control over its placement and size (this is pretty much the default state of affairs in many other UI toolkits, such as Delphi, Visual Basic, and so on).

To position the newly created delete button in the...

Connecting the dots

Our app already has a clear screen function but still draws just circles. Let's change it so that we can draw lines instead.

To follow continuous touch events (click-and-drag), we'll need to add a new event listener, on_touch_move. Every time the callback is invoked, it receives the latest point where the event occurred.

If we only had a single line going at every moment (like typically done on a desktop, since there is only one mouse pointer anyway), we could save the line we're currently drawing in self.current_line. But since we're aiming at multitouch support from the very beginning, we'll take another approach and store every line being drawn in the corresponding touch variable itself.

This works because for every continuous touch from start to end, all callbacks receive the same touch object. There is also a touch.ud property of the type dict (where ud is short for user data), which is specifically tailored to keep touch-specific attributes...

The color palette

Every painting program comes with a palette to choose colors from, and ours will be no exception by the time we reach the end of this section, real soon.

Conceptually, a palette is just a list of available colors, presented in a way that makes choosing the right color easy. In a full-fledged image editor, it usually includes every color available on the system (commonly a full 24-bit true color or the 16,777,216 unique colors). The customary representation of this all-encompassing palette typically looks like the following:

The color palette

Illustration of a true color palette window

On the other hand, if we aren't going to compete with popular proprietary image editing applications, we might as well ship a limited selection of colors. For a person with little to no background in graphics, this may even pose a competitive advantage—choosing fitting colors that look good together is hard. For this exact reason, there are palettes on the Internet that may be universally used for...

Setting the line thickness

The last and easiest feature that we are going to implement is a simple line thickness selector. As you can see in the following screenshot, we're reusing assets and styles from the previous part, the color palette.

Setting the line thickness

Line width selector

This UI uses yet another RadioButton subclass, unimaginatively named LineWidthButton. Append the following declaration to the paint.kv file:

<LineWidthButton@ColorButton>:
    group: 'line_width'
    on_release: app.canvas_widget.set_line_width(self.text)
    color: C('#2C3E50')
    background_color: C('#ECF0F1')

Key differences from ColorButton are highlighted in the preceding code. These new buttons belong to another radio group and fire another event handler when interacted with. Other than this, they are very similar.

The layout is equally simple, built in the same fashion as that of the color palette, except that it's vertical:

BoxLayout:
    orientation: 'vertical'
    padding...

Setting the stage


Initially, the entire surface of our app is occupied by the root widget, in this case, that's the canvas the user can paint upon. We won't devote any screen space to the instruments' area until later.

As you probably know by now, the root widget is the outermost widget in the hierarchy. Every Kivy application has one, and it can be pretty much anything, depending on the desired behavior. As seen in Chapter 1, Building a Clock App, BoxLayout is a suitable root widget; it was sufficient as we had no additional requirements for it, and layouts are designed to work as containers for other controls.

In the case of a Paint app, we need its root widget to adhere to much more interesting requirements; the user should be able to draw lines, possibly utilizing multitouch functionality, if available. At the moment, Kivy has no built-in controls suitable for the task at hand, so we will need to create our own.

Building new Kivy widgets is simple. As soon as our class inherits from Kivy...

Left arrow icon Right arrow icon
Download code icon Download Code

Description

This book is intended for programmers who are comfortable with the Python language and who want to build desktop and mobile applications with rich GUI in Python with minimal hassle. Knowledge of Kivy is not strictly required—every aspect of the framework is described when it's first used.

What you will learn

  • Set up a development environment for Python and Kivy programming
  • Build crossplatform applications suitable for desktop and mobile
  • Create Modern UI apps reminiscent of Windows Phone flat design
  • Interface with the native Android API to broaden the scope of what functionality is available to your apps
  • Customize your applications by modifying the builtin Kivy features for your project specifications
  • Develop fullstack, clientserver solutions with the backend and UI both written in Python
  • Write modular, reusable code while utilizing various aspects of the Kivy framework
  • Write your own crossplatform videogames, ready for distribution on Google Play, App Store, or even Steam

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jan 29, 2015
Length: 282 pages
Edition : 1st
Language : English
ISBN-13 : 9781783987856
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jan 29, 2015
Length: 282 pages
Edition : 1st
Language : English
ISBN-13 : 9781783987856
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₱260 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₱260 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 7,808.97
Kivy ??? Interactive Applications and Games in Python second edition
₱2500.99
Kivy Cookbook
₱2806.99
Kivy Blueprints
₱2500.99
Total 7,808.97 Stars icon
Banner background image

Table of Contents

11 Chapters
1. Building a Clock App Chevron down icon Chevron up icon
2. Building a Paint App Chevron down icon Chevron up icon
3. Sound Recorder for Android Chevron down icon Chevron up icon
4. Kivy Networking Chevron down icon Chevron up icon
5. Making a Remote Desktop App Chevron down icon Chevron up icon
6. Making the 2048 Game Chevron down icon Chevron up icon
7. Writing a Flappy Bird Clone Chevron down icon Chevron up icon
8. Introducing Shaders Chevron down icon Chevron up icon
9. Making a Shoot-Em-Up Game Chevron down icon Chevron up icon
A. The Python Ecosystem Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7
(10 Ratings)
5 star 40%
4 star 30%
3 star 10%
2 star 0%
1 star 20%
Filter icon Filter
Top Reviews

Filter reviews by




Spartacus Aug 15, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Book content is great. THIS BOOK is "For sale in India only" see photo. Blacked out but easily read through at an angle with a lamp.Sloooowwww shipping
Amazon Verified review Amazon
Munchkin Jun 06, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Proved to be very useful so far, providing some info that weren't plainly obvious from the standard tutorials. Anyway I find reading from paperbacks easier than pdfs. Maybe not as advanced as I'd hoped, so there is still a market out there for another book.
Amazon Verified review Amazon
Professor Guvinoff Aug 03, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The fastest way to learn any tool is to use it. This is as true today in interactive software as it has been all the way back to mastering the plow and building character at the same time. This is the reason I bought this book, and it did not disappoint. After toying with the examples given in the book, one is ready to further explore Kivy and the modern APIs it gives access to. Marl Vasilkov, like all good teachers, knows how to be funny while dealing with the tedium. I enthusiastically give him and his book six stars, because I feel he is one of these perennial children who love exceeding the known limits in everything they do.
Amazon Verified review Amazon
Jorge Carrillo May 16, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
As an experienced Python programmer I'm pretty happy with this book. As others have said this is not a book for beginners. You should know Python and a little bit of Kivy would be helpful as well.The book walks you through developing a series of progressively more challenging applications. The apps interface with other apps as is common these days. I appreciated the author's humor and directness. His explanations demonstrate a good breadth of technical knowledge.I downloaded the git hub code and followed along as I read this book. Other reviewers have commented on the readability of the kv files and their spacing. I had no problems. As far as far as typos in the book, kan anyone spel these days? Also not a big problem.
Amazon Verified review Amazon
Jean-François Parent Apr 09, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Kivy Blueprints cover from the ground up the Kivy multi-platform Python applications framework. This book include a lot of examples, some that show you the basic to get started using Kivy and some that illustrate more complex applications. The code's explanation is quite verbose and well written.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.