Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Kivy Cookbook

You're reading from   Kivy Cookbook Enhance your skills in developing multi-touch applications with Kivy

Arrow left icon
Product type Paperback
Published in Aug 2015
Publisher
ISBN-13 9781783987382
Length 246 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Hugo Solis Hugo Solis
Author Profile Icon Hugo Solis
Hugo Solis
Hugo Solis Hugo Solis
Author Profile Icon Hugo Solis
Hugo Solis
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Kivy and the Kv Language FREE CHAPTER 2. Input, Motion, and Touch 3. Events 4. Widgets 5. Graphics – Canvas and Instructions 6. Advanced Graphics – Shaders and Rendering 7. The API in Detail 8. Packaging our Apps for PC 9. Kivy for Mobile Devices 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.

You have been reading a chapter from
Kivy Cookbook
Published in: Aug 2015
Publisher:
ISBN-13: 9781783987382
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image