Kv language and kwargs

711 views
Skip to first unread message

Alexey Smishlayev

unread,
Feb 11, 2013, 3:40:58 AM2/11/13
to kivy-...@googlegroups.com
Hello!
Can I somehow pass additional keyword (or positional) arguments to the constructor using *.kv files? Thanks!

Cheers,
Alexey

Mathieu Virbel

unread,
Feb 11, 2013, 8:24:19 AM2/11/13
to kivy-...@googlegroups.com
Hi,

No, the kv language construct the widget dynamically. Usually in python,
we'll have:

btn = Button(text='hello')

While with kv:

<MyScreenWidget>:
Button:
text: 'hello'

Will result into this (internally):

instance = Factory.get('Button')()
# later during the processing of the rule
setattr(instance, 'text', 'hello')


The best way is to listen the property you are setting in the kv lang,
and do the python work from here.

Mathieu


Le 11/02/2013 09:40, Alexey Smishlayev a �crit :
> --
> You received this message because you are subscribed to the Google
> Groups "Kivy users support" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to kivy-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Alexey Smishlayev

unread,
Feb 11, 2013, 8:35:49 AM2/11/13
to kivy-...@googlegroups.com
Thanks for the reply!
I see, but could you, please, provide some examples on setting custom object properties using kv?
I've tried appending "prop: value"-formatted lines to my widgets, but it had no effect. Or do I have to somehow use/extend the Factory class you mentioned, or something else?

Cheers,
Alexey


On Monday, February 11, 2013 3:24:19 PM UTC+2, Mathieu Virbel wrote:
Hi,

No, the kv language construct the widget dynamically. Usually in python,
we'll have:

    btn = Button(text='hello')

While with kv:

    <MyScreenWidget>:
        Button:
           text: 'hello'

Will result into this (internally):

    instance = Factory.get('Button')()
    # later during the processing of the rule
    setattr(instance, 'text', 'hello')


The best way is to listen the property you are setting in the kv lang,
and do the python work from here.

Mathieu


Le 11/02/2013 09:40, Alexey Smishlayev a �crit :

Gabriel Pettier

unread,
Feb 14, 2013, 4:58:15 AM2/14/13
to kivy-...@googlegroups.com
Hi

Your properties must be defined as kivy properties for kv to be able to
set them.

from kivy.properties import ObjectProperty, NumericProperty, StringProperty


class MyWidget(Widget):
my_property = ObjectProperty(None)
some_other_property = NumericProperty(0)
some_text_properyt = StringProperty('')


this way the properties will have all the magical bindings that allow
you to set them from kv:

MyWidget:
my_property: my_property
some_other_property: 10
some_text_properyt: 'hello'

Button:
id: my_property

etc

cheers
> > Le 11/02/2013 09:40, Alexey Smishlayev a �crit :
> > > Hello!
> > > Can I somehow pass additional keyword (or positional) arguments to the
> > > constructor using *.kv files? Thanks!
> > >
> > > Cheers,
> > > Alexey
> > >
> > > --
> > > You received this message because you are subscribed to the Google
> > > Groups "Kivy users support" group.
> > > To unsubscribe from this group and stop receiving emails from it, send
> > > an email to kivy-users+...@googlegroups.com <javascript:>.

loaded_truss

unread,
Mar 5, 2013, 1:33:43 PM3/5/13
to kivy-...@googlegroups.com
You can only listen/access to properties defined in .kv only once the object has been constructed, right? What would you do if you needed to make a decision in the constructor?

For example, say you have two buttons which when pressed display a popup with one of two widgets in the popup, depending on the button. In .kv you'd have two different popup widgets to be able to do this. But if I could indicate to the constructor, then I can have one popup widget, and the popup constructor decides which popup content to create. What would be the best way to do this?

Thanks.



On Monday, February 11, 2013 8:24:19 AM UTC-5, Mathieu Virbel wrote:
Hi,

No, the kv language construct the widget dynamically. Usually in python,
we'll have:

    btn = Button(text='hello')

While with kv:

    <MyScreenWidget>:
        Button:
           text: 'hello'

Will result into this (internally):

    instance = Factory.get('Button')()
    # later during the processing of the rule
    setattr(instance, 'text', 'hello')


The best way is to listen the property you are setting in the kv lang,
and do the python work from here.

Mathieu


Le 11/02/2013 09:40, Alexey Smishlayev a �crit :
Reply all
Reply to author
Forward
0 new messages