xmzhang•

I have already puzzled with the code :)

well, first, two on_press statement in Line 22 and 23. when run the program, commonly Line 23 would override Line 22 because both of them called on_press, but however, it is not, both the Line 22 on_press and the Line 23 on_press would 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 first Factory.register('Label', module='kivy.uix.Label') then inherit from Factory.label???

2
terminakOP•

it is not, both the Line 22 on_press and the Line 23 on_press would be called .( unless I use return?? )

Nope - when you do on_press: <<something>>, it is conceptually the same as

def function_without_name(*largs):
    <<something>>

the_widget.bind(on_press=function_without_name)

You can't return from one to prevent the other.

shouldn't we first Factory.register('Label', module='kivy.uix.Label') then inherit from Factory.label???

All the widgets that come with Kivy are pre-registered with factory, so they are available without registering. Source

2
xmzhang•

thank you! so if I use on_press two times in kv, just like

# in kv
on_press: <<something1>>
on_press: <<something2>>

# in python
def function_without_name(*largs):
    <<something2>>
    <<something1>>

the_widget.bind(on_press=function_without_name)

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.

1
terminakOP•

They are independent callback functions, so more like

def function_without_name_1(*largs):
    <<something1>>
def function_without_name_2(*largs):
    <<something2>>

the_widget.bind(on_press=function_without_name_1)
the_widget.bind(on_press=function_without_name_2)

edit: although the effect is pretty much the same

2
xmzhang•

Thanks :) get it

1
terminakOP•

didn't catch that edit,

Factory is just a factory function to creat instance in kivy?

It is just a repository of name to class mappings, exactly like a dictionary:

import kivy
d = {'Widget': kivy.uix.widget.Widget}

When you do Factory.Widget it gives you the registered class, exactly like d['Widget'] with the dictionary above. This is implemented in Factory.__getattr__

2
xmzhang•

Wow,cool.though I really don't get the goal of such design patterns. it seemed quite awesome:)

2
enteleform•

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 : )

2