pythonkivy

float and NumericProperty in Kivy


I'm currently working on a Kivy project and I need to compare a Scatter position to the Screen size.

I've tried this line of code where self is the Scatter itself and GameScreen is the name of the Screen:

if self.pos[0] < 0 or self.pos[0] > GameScreen.width or self.pos[1] < 0 or self.pos[1] > GameScreen.height:
    # do something

The terminal yields this error:

TypeError: '>' not supported between instances of 'float' and 'kivy.properties.NumericProperty'.

I have also tried to mask the scatter position as a NumericProperty like this:

NumericProperty(self.pos[0])

but I get the same error as before:

TypeError: '>' not supported between instances of 'kivy.properties.NumericProperty' and 'kivy.properties.NumericProperty'

Finally I tried to transform the Screen width to a float by casting it like this:

float(GameScreen.width)

but I get the error TypeError: float() argument must be a string or a real number, not 'kivy.properties.NumericProperty'.

Do you have any advice or do you know how to do it?


Solution

  • You need to use an instance of GameScreen, not the class itself. Somewhere in your code you must create an instance of GameScreen. Use the width property of that instance.