-
-
Save anonymous/f0dd511b8d365de8290fe07260531bdc to your computer and use it in GitHub Desktop.
Kivy Factory and Kivy Language demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivy.base import runTouchApp | |
from kivy.lang import Builder | |
from kivy.factory import Factory | |
# ---------------------------------------- | |
# Widget classnames MUST start uppercase | |
# Kivy property names MUST start lowercase | |
# ---------------------------------------- | |
# WOW 1: Subclassing Magic (doesn't exist yet) + using mixin | |
# WOW 2: Creating new Kivy property "feet" for class Rabbit | |
# WOW 3: kvlang auto-listens to changes in Kivy property "feet" | |
# (text property is inherited from Label via Magic) | |
# WOW 4: Declare multiple callbacks for same event | |
# (on_press event is from ButtonBehavior mixin) | |
# WOW 5: Multi-line callback function (no further indents possible) | |
# WOW 6: Kivy properties emit events when they change, the | |
# event name is always on_<propertyname> | |
Builder.load_string(''' | |
<Rabbit@ButtonBehavior+Magic>: # WOW1 | |
feet: 4 # WOW2 | |
text: 'Rabbit with {} feet'.format(self.feet) # WOW3 | |
on_press: print('a callback') # WOW4 (with next line) | |
on_press: | |
self.feet = (self.feet + 1) % 5 | |
print("set feet: {}".format(self.feet)) # WOW5 | |
on_feet: | |
print("recv feet: {}".format(self.feet)) # WOW6 | |
''') | |
# WOW 7: Factory returns class objects registered with a | |
# particular name (which is a string, "Label" in this case). | |
class Magic(Factory.Label): | |
pass | |
# This is essentially the same as Factory.Label above, it | |
# returns the GridLayout class, but here we create an instance | |
# instead of inheriting (ie using function call syntax) | |
wow = Factory.GridLayout(cols=1) | |
# WOW 8: Widgets are auto-registered in Factory, so after | |
# declaring the Widget subclass, we can resolve it. | |
# (Kivy properties ("text" here) can be set at instantiation) | |
wow.add_widget(Factory.Magic(text='not a Rabbit')) | |
# WOW 9: The Rabbit class can be instantiated now, because the | |
# base class name "Magic" is resolvable by Factory. Since the | |
# Rabbit class is declared in kvlang (known as a "dynamic class"), | |
# this is the only way you can reach it outside kvlang. | |
wow.add_widget(Factory.Rabbit()) | |
# You can use both Rabbit and Magic is kvlang. This is a | |
# layer of indirection on top of Factory.Rabbit(). | |
wow.add_widget(Builder.load_string('Rabbit:')) | |
# WOW 10: You can mess around with factory too, if you want | |
# to get real fancy (as in, hacky). This is NOT endorsed, | |
# but it is sometimes practical.. | |
Factory.unregister('Label') # (NOT ENDORSED) | |
Factory.register('Label', cls=Factory.Rabbit) # (NOT ENDORSED) | |
wow.add_widget(Builder.load_string('Label:')) # now a Rabbit | |
runTouchApp(wow) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment