Skip to content

Instantly share code, notes, and snippets.

@me2beats
Created August 22, 2019 23:49
Show Gist options
  • Save me2beats/0f276c4097215edb6edbef4c27ea0525 to your computer and use it in GitHub Desktop.
Save me2beats/0f276c4097215edb6edbef4c27ea0525 to your computer and use it in GitHub Desktop.
bind event to every class instance?
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
def before(self, *_):
self.bind(**{'on_touch_down' : test})
def test(*_):
print ('cool!')
def my_decorator(func, before):
def inner_before(*args, **kwargs):
before(*args)
func(*args, **kwargs)
return inner_before
KV = """
BoxLayout
MyLabel
MyLabel
MyLabel
<MyLabel>
text: 'touch me'
"""
class MyLabel(Label):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class MyApp(App):
def build(self):
MyLabel.__init__ = my_decorator(MyLabel.__init__, before = before)
return Builder.load_string(KV)
MyApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment