-
-
Save gottadiveintopython/bd47909fc010fc17ee552a9db77449a1 to your computer and use it in GitHub Desktop.
Qiita記事 EventDispatcherのeventを待機
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.clock import Clock | |
def start_gen(gen): | |
def step_gen(*args, **kwargs): | |
try: | |
gen.send((args, kwargs, ))(step_gen) | |
except StopIteration: | |
pass | |
try: | |
gen.send(None)(step_gen) | |
except StopIteration: | |
pass | |
def event(ed, name): | |
bind_id = None | |
step_gen = None | |
def bind(step_gen_): | |
nonlocal bind_id, step_gen | |
bind_id = ed.fbind(name, callback) | |
assert bind_id > 0 # bindingに成功したか確認 | |
step_gen = step_gen_ | |
def callback(*args, **kwargs): | |
ed.unbind_uid(name, bind_id) | |
step_gen(*args, **kwargs) | |
return bind | |
from kivy.app import App | |
from kivy.lang import Builder | |
from kivy.factory import Factory | |
def some_task(label): | |
yield event(label, 'on_touch_down') | |
label.text = 'B' | |
yield event(label, 'on_touch_down') | |
label.text = 'C' | |
KV_CODE = ''' | |
Label: | |
text: 'A' | |
''' | |
class SampleApp(App): | |
def build(self): | |
return Builder.load_string(KV_CODE) | |
def on_start(self): | |
start_gen(some_task(self.root)) | |
if __name__ == '__main__': | |
SampleApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment