2012-07-16 15:22:19 +00:00
|
|
|
Properties
|
|
|
|
----------
|
2012-04-04 10:04:52 +00:00
|
|
|
|
2013-05-07 06:18:15 +00:00
|
|
|
Kivy introduces a new way of declaring properties within a class.
|
2012-07-14 12:10:14 +00:00
|
|
|
Before::
|
|
|
|
|
|
|
|
class MyClass(object):
|
|
|
|
def __init__(self):
|
|
|
|
super(MyClass, self).__init__()
|
|
|
|
self.numeric_var = 1
|
|
|
|
|
|
|
|
After, using Kivy's properties::
|
|
|
|
|
|
|
|
class MyClass(EventDispatcher):
|
|
|
|
numeric_var = NumericProperty(1)
|
|
|
|
|
|
|
|
Theses properties implement the `Observer pattern
|
2013-05-07 06:18:15 +00:00
|
|
|
<http://en.wikipedia.org/wiki/Observer_pattern>`_. They help you to:
|
2012-04-04 10:04:52 +00:00
|
|
|
|
2013-05-07 06:18:15 +00:00
|
|
|
- Easily manipulate widgets defined in the :doc:`/guide/lang`
|
2012-04-18 19:12:50 +00:00
|
|
|
- Automatically observe any changes and dispatch functions/code accordingly
|
2012-07-08 09:36:49 +00:00
|
|
|
- Check and validate values
|
|
|
|
- Optimize memory management
|
2012-04-04 10:04:52 +00:00
|
|
|
|
|
|
|
|
2012-05-28 16:42:11 +00:00
|
|
|
To use them, **you have to declare them at class level**. That is, directly in
|
2012-07-14 12:10:14 +00:00
|
|
|
the class, not in any method of the class. A property is a class attribute
|
2012-05-28 16:42:11 +00:00
|
|
|
that will automatically create instance attributes. Each property by default
|
2012-07-14 12:10:14 +00:00
|
|
|
provides an ``on_<propertyname>`` event that is called whenever the property's
|
2013-06-07 22:32:04 +00:00
|
|
|
state/value changes.
|
2012-04-04 10:04:52 +00:00
|
|
|
|
2012-04-18 19:12:50 +00:00
|
|
|
Kivy provides the following properties:
|
2012-07-23 23:08:53 +00:00
|
|
|
:mod:`~kivy.properties.NumericProperty`,
|
|
|
|
:mod:`~kivy.properties.StringProperty`,
|
|
|
|
:mod:`~kivy.properties.ListProperty`,
|
|
|
|
:mod:`~kivy.properties.ObjectProperty`,
|
|
|
|
:mod:`~kivy.properties.BooleanProperty`,
|
|
|
|
:mod:`~kivy.properties.BoundedNumericProperty`,
|
|
|
|
:mod:`~kivy.properties.OptionProperty`,
|
|
|
|
:mod:`~kivy.properties.ReferenceListProperty`,
|
|
|
|
:mod:`~kivy.properties.AliasProperty`,
|
|
|
|
:mod:`~kivy.properties.DictProperty`,
|
2012-07-14 12:10:14 +00:00
|
|
|
|
2013-06-07 22:32:04 +00:00
|
|
|
For an in-depth explaination, take a look at :doc:`/api-kivy.properties`.
|