Created
August 22, 2019 23:49
-
-
Save me2beats/0f276c4097215edb6edbef4c27ea0525 to your computer and use it in GitHub Desktop.
bind event to every class instance?
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
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