-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hide already-awaited coroutines from the user
- Loading branch information
1 parent
ad1d35e
commit b48d8cc
Showing
2 changed files
with
62 additions
and
7 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from kivy.lang import Builder | ||
from kivy.app import App | ||
from kivy.uix.button import Button | ||
import asynckivy as ak | ||
|
||
|
||
KV_CODE = ''' | ||
BoxLayout: | ||
orientation: 'vertical' | ||
Label: | ||
id: label | ||
font_size: 50 | ||
Button: | ||
id: button | ||
font_size: 50 | ||
''' | ||
|
||
class TestApp(App): | ||
def build(self): | ||
return Builder.load_string(KV_CODE) | ||
def on_start(self): | ||
async def some_task(): | ||
label = self.root.ids.label | ||
button = self.root.ids.button | ||
label.text = '--' | ||
button.text = 'start spinning' | ||
await ak.event(button, 'on_press') | ||
button.text = 'stop' | ||
tasks = await ak.or_( | ||
ak.event(button, 'on_press'), | ||
spinning(label), | ||
) | ||
tasks[1].coro.close() # 'tasks[1].cancel()' is preferable | ||
self.root.remove_widget(button) | ||
label.text = 'fin.' | ||
ak.start(some_task()) | ||
|
||
|
||
async def spinning(label): | ||
import itertools | ||
sleep_for_10th_of_a_second = await ak.create_sleep(.1) | ||
for stick in itertools.cycle('\ | / --'.split()): | ||
label.text = stick | ||
await sleep_for_10th_of_a_second() | ||
|
||
|
||
if __name__ == '__main__': | ||
TestApp().run() |