Skip to content

Instantly share code, notes, and snippets.

@gottadiveintopython
Last active December 4, 2019 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gottadiveintopython/d40b5d71ed03fc8370a541e3fe8263b0 to your computer and use it in GitHub Desktop.
Save gottadiveintopython/d40b5d71ed03fc8370a541e3fe8263b0 to your computer and use it in GitHub Desktop.
Qiita記事 event()とsleep()を混ぜて使う
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 sleep(duration):
return lambda step_gen: Clock.schedule_once(step_gen, duration)
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(button):
yield event(button, 'on_press')
button.text = 'Pressed'
yield sleep(1)
button.text = 'Bye'
KV_CODE = '''
Button:
text: 'Press Me'
'''
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