Pergunta

I am trying to use a NumericProperty but getting Type errors when trying to use it as a value

My code looks like this

from kivy.properties import NumericProperty
from kivy.uix.widget import Widget

class Segment(Widget):
def __init__(self, segments):
    super(Segment, self).__init__()
    self.segments = NumericPropery(segments)

def build(self):
for i in range(0, self.segments):
    # Do something

I get an error :

for i in range(0, self.segments):
TypeError: range() integer end argument expected, got kivy.properties.NumericProperty.

so I tried using self.segments.get() instead, but then I got this error

TypeError: get() takes exactly one argument (0 given)

apperently the get function expects <kivy._event.EventDispatcher> object argument

Any idea how to get around this?

Foi útil?

Solução 2

You have to declare properties at class level.

class Segment(Widget):
    segments = NumericProperty()

This will give the correct behaviour. The problem is that properties do their own management of per-instance values and interacting with the eventloop etc.. If you don't declare them at class level, they don't get to do this, so your functions only see the NumericProperty itself (which is your problem).

Outras dicas

I had a similar problem with this code ...

class GameModel(object):
    some_number = NumericProperty(1)
    def __init__(self):
        self.some_number = 2

... which raised the error:

TypeError: Argument 'obj' has incorrect type (expected kivy._event.EventDispatcher, got GameModel)

I did declare the property at class level though. In my case the problem was that the class itself was not derived from a Kivy Widget class or - as stated in the error message - from an EventDispatcher Object

Deriving from EventDispatcher fixed my problem:

class GameModel(EventDispatcher):

Hope this is helpful for someone else some day ;-)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top