widget: add widget metaclass to automatically register widget classes into the Factory.

This commit is contained in:
Mathieu Virbel 2012-10-10 00:30:25 +02:00
parent 7c96bfa504
commit 7fe67a3ba0
1 changed files with 14 additions and 0 deletions

View File

@ -59,6 +59,7 @@ widget moves, you can bind your own callback function like this::
__all__ = ('Widget', 'WidgetException') __all__ = ('Widget', 'WidgetException')
from kivy.event import EventDispatcher from kivy.event import EventDispatcher
from kivy.factory import Factory
from kivy.properties import NumericProperty, StringProperty, \ from kivy.properties import NumericProperty, StringProperty, \
AliasProperty, ReferenceListProperty, ObjectProperty, \ AliasProperty, ReferenceListProperty, ObjectProperty, \
ListProperty ListProperty
@ -73,6 +74,17 @@ class WidgetException(Exception):
pass pass
class WidgetMetaclass(type):
'''Metaclass to auto register new widget into :class:`~kivy.factory.Factory`
.. warning::
This metaclass is used for Widget. Don't use it directly !
'''
def __init__(mcs, name, bases, attrs):
super(WidgetMetaclass, mcs).__init__(name, bases, attrs)
Factory.register(name, cls=mcs)
class Widget(EventDispatcher): class Widget(EventDispatcher):
'''Widget class. See module documentation for more information. '''Widget class. See module documentation for more information.
@ -91,6 +103,8 @@ class Widget(EventDispatcher):
''' '''
__metaclass__ = WidgetMetaclass
def __init__(self, **kwargs): def __init__(self, **kwargs):
# Before doing anything, ensure the windows exist. # Before doing anything, ensure the windows exist.
EventLoop.ensure_window() EventLoop.ensure_window()