I Just started to learn kivy and I am very confused on the usage of the ObjectProperty class, and how it takes None as an argument. Could somebody explain it please? I found
The Kivy Property
is a convenience class similar to Python's own property
but that also provides type checking, validation and events. ObjectProperty
is a specialised sub-class of the Property
class, so it has the same initialisation parameters as it:
By default, a Property always takes a default value[.] The default value must be a value that agrees with the Property type. For example, you can’t set a list to a StringProperty, because the StringProperty will check the default value.
None is a special case: you can set the default value of a Property to None, but you can’t set None to a property afterward. If you really want to do that, you must declare the Property with allownone=True[.]
(from the Kivy Property documentation)
In your code, PongGame
has a ball
property that is initially set to None
and will later be assigned a ball object. This is defined in the kv file:
<PongGame>:
ball: pong_ball
PongBall:
id: pong_ball
center: self.parent.center
Because no object was passed to the initialiser, any object can be assigned to that property. You could restrict it to only hold ball objects by initialising it with a dummy value:
ball = ObjectProperty(PongBall())