Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Kivy sends `on_touch_down` events to all widgets
- # so every widget has to check if touch was made in widget's area.
- # Or you have to use `on_press` or `on_release` instead `on_touch_down`
- # https://stackoverflow.com/a/45942518/1832058
- #--- calc.kv ---
- #:kivy 1.10.0
- <Calculate>:
- display: entry
- rows: 5
- padding: 10
- spacing: 10
- BoxLayout:
- TextInput:
- id: entry
- multiline: False
- font_size: 32
- text: "Kivy Touch: "
- BoxLayout:
- spacing: 10
- Button:
- font_size: 20
- text: '7'
- on_press: entry.text += self.text
- Button:
- font_size: 20
- text: '8'
- on_release: entry.text += self.text
- Button:
- font_size: 20
- text: '9'
- on_touch_down: root.touch_button(*args)
- Button:
- font_size: 20
- text: '+'
- on_touch_down: root.touch_button(*args)
- #--- calc.py ---
- #!/usr/bin/env python3
- from kivy.app import App
- from kivy.uix.gridlayout import GridLayout
- # kivy sends `on_touch_down` events to all widgets
- # so every widget has to check if touch was made in widget's area.
- # Or you have to use `on_press` or `on_release` instead `on_touch_down`
- # https://stackoverflow.com/a/45942518/1832058
- class Calculate(GridLayout):
- def touch_button(self, instance, touch, *args):
- if instance.collide_point(*touch.pos):
- self.ids.entry.text += instance.text
- return False # don't send event to other buttons
- class CalcApp(App):
- def build(self):
- return Calculate()
- if __name__ == '__main__':
- CalcApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement