Skip to content

Instantly share code, notes, and snippets.

@kived
Created October 6, 2014 17:56
Show Gist options
  • Save kived/16b9c2391532f43ec895 to your computer and use it in GitHub Desktop.
Save kived/16b9c2391532f43ec895 to your computer and use it in GitHub Desktop.
Kivy: increment/decrement while button pressed
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()
@MaKarf
Copy link

MaKarf commented Jul 9, 2021

Thanks for the code. It really helped me figured out what I needed to do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment