About custom properties defined for widgets in kv language

794 views
Skip to the first unread message

Jason Molina

unread,
21 Feb 2015, 10:20:09 am21/02/2015
to kivy-...@googlegroups.com
Hello. It seems that if I define a custom property for a widget in a kv file the rest of widgets of the same class get that property defined with the same value, Is this expected?

I have this code that prints the value of property custom_prop for the two Buttons defined in the kv string.

Code:

from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.clock import Clock

kv="""
BoxLayout:
    Button:
        text: 'button1'
        custom_prop: 'prop_value'
    Button:
        text: 'button2'
"""

def test(t):
    for widget in root.children:
        print "%s: %s"%(widget.text, widget.custom_prop)

root = Builder.load_string(kv)
Clock.schedule_once(test, 1)
runTouchApp(root)


After running the code I get the following output:

button2: prop_value
button1: prop_value 

I was expecting an exception for Button 2 or an empty value.

I'm using kivy 1.8.0 on a linux machine 64-bit.  Thank you.

Alexander Hupfer

unread,
19 May 2016, 7:15:13 am19/05/2016
to Kivy users support
I see the same behavior, it gets even more weired:

kv="""
BoxLayout:
    Button:
        text: 'button1'
        custom_prop: 'prop_value'
    Button:
        text: 'button2'
    Button:
        text: 'button3'
        custom_prop: 'prop_value_3'
    Button:
        text: 'button4'

"""

gets you the output, so the first button becomes to default value, which does not change thereafter:

button4: prop_value
button3: prop_value_3
button2: prop_value
button1: prop_value

Alexander Taylor

unread,
19 May 2016, 7:22:38 am19/05/2016
to Kivy users support
Since properties exist at class level, the first kv-only property causes the property to be added to the class, with the default value being whatever is first passed. That means that all instances of the class gain that property and default value.

Alexander Hupfer

unread,
19 May 2016, 7:28:41 am19/05/2016
to Kivy users support
Thank you for the clarification. Would there be a way to define a default value different from what was passed as a first value in the kv file?

Alexander Taylor

unread,
19 May 2016, 7:30:01 am19/05/2016
to kivy-...@googlegroups.com
Yes, by creating the property in python code. It probably also works to
declare the property twice, and pass the default value the first time.
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Kivy users support" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/kivy-users/rkZ5nJiVXQU/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> kivy-users+...@googlegroups.com
> <mailto:kivy-users+...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.


signature.asc

Alexander Hupfer

unread,
19 May 2016, 7:32:57 am19/05/2016
to Kivy users support
I tried something like this, but this doesn't seem to affect the default value:

from kivy.uix import button
button.custom_prop = None

kv="""....

Alexander Taylor

unread,
19 May 2016, 9:41:08 am19/05/2016
to kivy-...@googlegroups.com
You can't add a property like that, nor should you really. Make your own
Button subclass with the property you want.

If you really want to, you can use widget.create_property to add a
property dynamically, but I think it is bad practice - you should make
your own subclasses to style widgets, not modify the builtin ones (which
could have weird side effects later).
> <https://groups.google.com/d/topic/kivy-users/rkZ5nJiVXQU/unsubscribe>.
> > To unsubscribe from this group and all its topics, send an email to
> > kivy-users+...@googlegroups.com <javascript:>
> > <mailto:kivy-users+...@googlegroups.com <javascript:>>.
> > For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Kivy users support" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/kivy-users/rkZ5nJiVXQU/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> kivy-users+...@googlegroups.com
> <mailto:kivy-users+...@googlegroups.com>.
signature.asc
Reply all
Reply to author
Forward
0 new messages