Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Kivy Cookbook
  • Toc
  • feedback
Kivy Cookbook

Kivy Cookbook

By : Hugo Solis, Solis
2.5 (2)
close
Kivy Cookbook

Kivy Cookbook

2.5 (2)
By: Hugo Solis, Solis

Overview of this book

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.
Table of Contents (11 chapters)
close
10
Index

Declaring properties within a class

Here we want to highlight an important difference between traditional Python coding and Kivy, and the usefulness of this change.

Getting ready

We need to remember the traditional form to declare properties in Python. Usually, if we want to declare a property in Python, we do something such as:

class MyClass(object):
    def __init__(self):
        super(MyClass,self).__init__()
        self._numeric_var = 1
@property
    def numeric_var(self): 
        return self._numeric_var

We are declaring a numeric one, whereas if we use MyClass().numeric_var in the Python shell, we get 1 in return.

How to do it…

Now, to declare this property in Kivy we follow these steps:

  1. Import Kivy and its properties
  2. Define the class
  3. Reference the Kivy property, in this case the numeric one:
    import kivy
    
    from kivy.event import EventDispatcher
    from kivy.properties import *
    
    class MyClass(EventDispatcher):
        numeric_var = NumericProperty(1.0)

How it works…

The idea behind this is that you inherit the declaration from Kivy's properties, which reduces the number of code lines.

To use them, you have to declare them at a class level. That is, directly in the class, not in any method for the class. A property is a class attribute that will automatically create instance attributes. Each property, by default, provides an on_<propertyname> event that is called whenever the property's state/value changes.

Something additional to point out is that NumericProperty accepts all the Python numeric values: ints, floats, and longs.

In general, Kivy properties can be overridden easily when creating the instance of the class, using keyword arguments such as ClassName(property=newvalue).

There's more…

They help you to:

  • Easily manipulate widgets defined in the Kv language
  • Automatically observe any changes
  • Check and validate values
  • Optimize memory management

Kivy provides more properties as follows:

  • NumericProperty
  • StringProperty
  • ListProperty
  • ObjectProperty
  • BooleanProperty
  • BoundedNumericProperty
  • OptionProperty
  • ReferenceListProperty
  • AliasProperty
  • DictProperty

See also

These properties actually implement the Observer pattern; if you want to learn more about patterns, you can find information online at http://www.oodesign.com/observer-pattern.html.

bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete