Reputation: 121
I have a number that i want to range from 0 to 100, no more and no less. I tried setting the number as:
ego = NumericProperty(0, min=0, max=100)
However, the number still allows itself to go over 100 when I press this button:
on_release: root.update_ego()
Button:
text: "increase ego"
pos: 700,500
on_release: root.update_ego()
And my .py file says this:
def update_ego(self):
self.ego += 1
Upvotes: 2
Views: 605
Reputation: 11
Just as an update, for others that drunkenly stumble in here, you can totally update variables in the kivy button itself.
Button:
text: "increase ego"
on_release: root.ego = root.ego + 1
This can cut down the number of functions you have, and save you from unneeded logic hurdles.
Upvotes: 0
Reputation: 16031
You should do the following:
from kivy.properties import BoundedNumericProperty
...
# returns the boundary value when exceeded
ego = BoundedNumericProperty(0, min=0, max=100,
errorhandler=lambda x: 100 if x > 100 else 0)
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BoundedNumericProperty
class RootWidget(BoxLayout):
ego = BoundedNumericProperty(0.0, min=0.0, max=2.0,
errorhandler=lambda x: 2.0 if x > 2.0 else 0.0)
def update_ego(self):
print('before increment: ego={0:5.2f}'.format(self.ego))
self.ego += 1.0
print('after increment: ego={0:5.2f}'.format(self.ego))
class Test2App(App):
def build(self):
return RootWidget()
if __name__ == "__main__":
Test2App().run()
#:kivy 1.10.0
<RootWidget>:
orientation: "vertical"
Label:
font_size: 70
center_x: root.width / 4
top: root.top - 50
text: "{0:5.2f}".format(root.ego)
Button:
text: "increase ego"
on_release: root.update_ego()
Upvotes: 0
Reputation: 76
Since I don't know the cause of the problem from this (maybe you have to set the number at another place in the code), I suggest this workaround:
def update_ego(self):
if self.ego < 100:
self.ego += 1
Upvotes: 2