Skip to content

Instantly share code, notes, and snippets.

@yaki29
Last active March 24, 2019 05:14
Show Gist options
  • Save yaki29/5371972603cdbba444bf2effc6eaa6cb to your computer and use it in GitHub Desktop.
Save yaki29/5371972603cdbba444bf2effc6eaa6cb to your computer and use it in GitHub Desktop.
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
from kivy.uix.button import Button
from functools import partial
class DemoBox(BoxLayout):
def __init__(self, **kwargs):
super(DemoBox, self).__init__(**kwargs)
self.orientation = "vertical"
btn = Button(text="Normal binding to event")
btn.fbind('on_press', self.on_event)
btn2 = Button(text="Normal binding to a property change")
btn2.fbind('state', self.on_property)
btn3 = Button(text="A: Using function with args.")
btn3.fbind('on_press', self.on_event_with_args, 'right',
tree='birch', food='apple')
btn4 = Button(text="Unbind A.")
btn4.fbind('on_press', self.unbind_a, btn3)
btn5 = Button(text="Use a flexible function")
btn5.fbind('on_press', self.on_anything)
btn6 = Button(text="B: Using flexible functions with args. For hardcores.")
btn6.fbind('on_press', self.on_anything, "1", "2", monthy="python")
btn7 = Button(text="Force dispatch B with different params")
btn7.fbind('on_press', btn6.dispatch, 'on_press', 6, 7, monthy="other python")
btn8 = Button(text="bind A")
btn8.fbind('on_press', self.bind_a, self.on_event_with_args ,btn3)
for but in [btn, btn2, btn3, btn4, btn5, btn6, btn7, btn8]:
self.add_widget(but)
def on_event(self, obj):
print("Typical event from", obj)
def on_event_with_args(self, side, obj, tree=None, food=None):
print("Event with args", obj, side, tree, food)
def on_property(self, obj, value):
print("Typical property change from", obj, "to", value)
def on_anything(self, *args, **kwargs):
print('The flexible function has *args of', str(args),
"and **kwargs of", str(kwargs))
return True
def unbind_a(self, btn, event):
btn.funbind('on_press', self.on_event_with_args, 'right',
tree='birch', food='apple')
print("unbound")
def bind_a(self, obj, btn, event):
btn.fbind('on_press', event, 'right',
tree='birch', food='apple')
print("bound")
class DemoApp(App):
def build(self):
return DemoBox()
if __name__ == "__main__":
DemoApp().run()
@me2beats
Copy link

Have this traceback after 'Bind A'-'Unbind A' or 'A'-'Bind A'-'A'
Is this ok?

   File "main.py", line 69, in <module>
     DemoApp().run()
   File "/usr/lib/python3/dist-packages/kivy/app.py", line 855, in run
     runTouchApp()
   File "/usr/lib/python3/dist-packages/kivy/base.py", line 502, in runTouchApp
     EventLoop.window.mainloop()
   File "/usr/lib/python3/dist-packages/kivy/core/window/window_sdl2.py", line 724, in mainloop
     self._mainloop()
   File "/usr/lib/python3/dist-packages/kivy/core/window/window_sdl2.py", line 459, in _mainloop
     EventLoop.idle()
   File "/usr/lib/python3/dist-packages/kivy/base.py", line 340, in idle
     self.dispatch_input()
   File "/usr/lib/python3/dist-packages/kivy/base.py", line 325, in dispatch_input
     post_dispatch_input(*pop(0))
   File "/usr/lib/python3/dist-packages/kivy/base.py", line 231, in post_dispatch_input
     listener.dispatch('on_motion', etype, me)
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "/usr/lib/python3/dist-packages/kivy/core/window/__init__.py", line 1360, in on_motion
     self.dispatch('on_touch_down', me)
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "/usr/lib/python3/dist-packages/kivy/core/window/__init__.py", line 1376, in on_touch_down
     if w.dispatch('on_touch_down', touch):
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "/usr/lib/python3/dist-packages/kivy/uix/widget.py", line 460, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "/usr/lib/python3/dist-packages/kivy/uix/behaviors/button.py", line 151, in on_touch_down
     self.dispatch('on_press')
   File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
   File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
   File "kivy/_event.pyx", line 1088, in kivy._event.EventObservers._dispatch
 TypeError: 'Button' object is not callable

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