Just to be particularly explicit about ZenCODE's explanation, kivy's
property system explicitly does extra stuff above and beyond python's
natural class variable behaviour. For something like 'self.value =
NumericProperty(0)', the Property adds extra behaviour that *does* give
individual class instances their own copy of the variable, but this is
solely because kivy takes care of it. In your example you just did the
normal python behaviour with no properties, 'self.wgt = Widget()', so
you got the standard shared instance.
It's possible that you can do something like 'wgt =
ObjectProperty(Widget())' to get what you want by using kivy's property
system (but I'm not sure if that would actually copy the widget
instance...), but even if so it throws up some problems and probably
still breaks the behaviour you originally wanted.
I'm not quite sure what your example really intends, it seems like a mix
of two ideas. Do you really want to do something like:
class MyClass(Widget):
wx = NumericProperty(0)
def __init__(self, *args):
super(Myclass, self).__init__(*args)
wgt = Widget()
wgt.x = self.wx
This would create a new widget ('wgt') when MyClass is instantiated, and
assign its property 'x' (the x position) to be the wx property of
MyClass. You could then do whatever you like with the widget.
A few notes on this though, it probably still doesn't do what you really
intend:
1) The code doesn't actually do anything further with wgt (neither does
any of your example code). If you want to display it, you probably want
to do 'self.add_widget(wgt)' after creating it.
2) In your last post, 'self.wgt.size = value' will fail because size is
actually a list (or maybe a tuple, whatever) or two values for the width
and height respectively. That's why I replaced it with the example
property 'x', which is a single number controlling the x position of the
widget.
3) Using kv language makes much of the creation and binding of widgets
very easy, rather than mucking around in python, though of course it's
worth understanding both ways. For instance, if you want every instance
of MyClass to have a child widget, you could easily write that in kv
instead of fiddling with __init__.
On 20/08/13 09:38, Damien Frikha wrote:
> Well then, if any variable declared outside of the __init__ method is a class variable shared by all instances of the class, how come every built-it widget defines its main attributes with this type of variable (a slider for instance has "value", "orientation", "padding"... defined as class variables) whereas they aren't exactly shared attributes since they change with instances. It came to my understanding that this variables were defined like that because their values are defined before the __init__ call, like when you define a slider by slider = Slider(value=15), the value fails to be attributed if the "value" variable is declared in __init__.
>
> What I want do is to set an attribute of the "wgt" Widget at the creation of the MyClass instance, something like that :
>
> class MyClass(Widget):
>
> wgt = Widget()
>