Kivy Factory and Kivy Language demo snippet with notes
gist.github.com/anonymous/f0dd511b8d365de8290...Posted Jul 4, 2017, 12:12 PM UTC
69
Posted Jul 4, 2017, 12:12 PM UTC
This is awesome, didn't know about Factory.
Been working on a wrapper that has a full set of widgets, subclassed & expanded from Kivy & KivyMD. Was likely gonna do something hackish to avoid tons of imports @ *.kv files, but Factory.register(...) just made my life a lot easier : )
I have already puzzled with the code :)
well, first, two
on_pressstatement in Line 22 and 23. when run the program, commonly Line 23 would override Line 22 because both of them calledon_press, but however, it is not, both the Line 22on_pressand the Line 23on_presswould be called .( unless I use return?? )My thought, If I define class in kv, when I want to use that class, I use Factory. but it seemed I really know nothing about factory. Line 32, directly use
Factory.label, shouldn't we firstFactory.register('Label', module='kivy.uix.Label')then inherit from Factory.label???Nope - when you do
on_press: <<something>>, it is conceptually the same asYou can't return from one to prevent the other.
All the widgets that come with Kivy are pre-registered with factory, so they are available without registering. Source
thank you! so if I use on_press two times in kv, just like
right?
EDIT: and Factory is just a factory function to creat instance in kivy? if I use Factory, I can directly use Factory.Label without
from kivy.uix.label import Label.They are independent callback functions, so more like
edit: although the effect is pretty much the same
Thanks :) get it
didn't catch that edit,
It is just a repository of name to class mappings, exactly like a dictionary:
When you do
Factory.Widgetit gives you the registered class, exactly liked['Widget']with the dictionary above. This is implemented inFactory.__getattr__Wow,cool.though I really don't get the goal of such design patterns. it seemed quite awesome:)