
Kivy Cookbook
By :

Here we want to highlight an important difference between traditional Python coding and Kivy, and the usefulness of this change.
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.
Now, to declare this property in Kivy we follow these steps:
import kivy from kivy.event import EventDispatcher from kivy.properties import * class MyClass(EventDispatcher): numeric_var = NumericProperty(1.0)
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)
.
They help you to:
Kivy provides more properties as follows:
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.
Change the font size
Change margin width
Change background colour