Created
October 6, 2014 17:56
-
-
Save kived/16b9c2391532f43ec895 to your computer and use it in GitHub Desktop.
Kivy: increment/decrement while button pressed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kivy | |
kivy.require('1.8.1') | |
from kivy.app import App | |
from kivy.lang import Builder | |
from kivy.clock import Clock | |
from kivy.properties import NumericProperty | |
class TestApp(App): | |
value = NumericProperty(0) | |
def build(self): | |
return Builder.load_string(''' | |
BoxLayout: | |
orientation: 'vertical' | |
Label: | |
text: str(app.value) | |
canvas.before: | |
Color: | |
rgba: app.value / 64., (64 - app.value) / 64., 0, 1 | |
Rectangle: | |
pos: self.pos | |
size: self.size | |
BoxLayout: | |
Button: | |
text: '-' | |
on_press: app.minus() | |
on_release: app.stopupdate() | |
Button: | |
text: '+' | |
on_press: app.plus() | |
on_release: app.stopupdate() | |
''') | |
def plus(self): | |
self.event = Clock.schedule_interval(lambda dt: setattr(self, 'value', self.value + 1), 0.1) | |
self.value += 1 | |
def minus(self): | |
self.event = Clock.schedule_interval(lambda dt: setattr(self, 'value', self.value - 1), 0.1) | |
self.value -= 1 | |
def stopupdate(self): | |
self.event.cancel() | |
if __name__ == '__main__': | |
TestApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the code. It really helped me figured out what I needed to do