File tree Expand file tree Collapse file tree 1 file changed +10
-10
lines changed Expand file tree Collapse file tree 1 file changed +10
-10
lines changed Original file line number Diff line number Diff line change 44
55from async_tkinter_loop import async_mainloop
66
7+ event = asyncio .Event ()
8+
79
810async def counter ():
911 i = 0
1012 while True :
11- await event .wait ()
13+ await event .wait () # pauses if `event` object is cleared, unpauses if it is set
1214 i += 1
1315 label .config (text = str (i ))
1416 await asyncio .sleep (1.0 )
1517
1618
17- root = tk .Tk ()
18-
19- label = tk .Label (root )
20- label .pack ()
21-
22- event = asyncio .Event ()
23-
24-
2519def start_stop ():
2620 if event .is_set ():
2721 event .clear ()
2822 else :
2923 event .set ()
3024
3125
26+ root = tk .Tk ()
27+
28+ label = tk .Label (root )
29+ label .pack ()
30+
3231tk .Button (root , text = "Start/stop" , command = start_stop ).pack ()
3332
34- # Start background task
33+ # Create a new asyncio event loop, then run `counter()` as a background task
3534event_loop = asyncio .new_event_loop ()
3635task = event_loop .create_task (counter ())
3736
37+ # `event_loop` should be passed to `async_mainloop` in this case, so that it won't create a new event loop
3838async_mainloop (root , event_loop )
You can’t perform that action at this time.
0 commit comments