Skip to content

Commit a3f1dd7

Browse files
authored
Update start_stop_counter.py
1 parent 661c5c9 commit a3f1dd7

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

examples/start_stop_counter.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,35 @@
44

55
from async_tkinter_loop import async_mainloop
66

7+
event = asyncio.Event()
8+
79

810
async 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-
2519
def 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+
3231
tk.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
3534
event_loop = asyncio.new_event_loop()
3635
task = 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
3838
async_mainloop(root, event_loop)

0 commit comments

Comments
 (0)