forked from smontanaro/tk-asyncio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
29 lines (24 loc) · 897 Bytes
/
__init__.py
File metadata and controls
29 lines (24 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import asyncio
from tkinter import Tk, Button
class AsyncTk(Tk):
"Basic Tk with an asyncio-compatible event loop"
def __init__(self):
super().__init__()
self.running = True
self.runners = [self.tk_loop()]
self.button_presses = []
async def tk_loop(self):
"asyncio 'compatible' tk event loop?"
# Is there a better way to trigger loop exit than using a state vrbl?
while self.running:
self.update()
await asyncio.sleep(0.05) # obviously, sleep time could be parameterized
if len(self.button_presses) > 0:
await self.button_presses.pop(0)
def stop(self):
self.running = False
async def run(self):
await asyncio.gather(*self.runners)
def add_button_coro(self, coro):
task = asyncio.create_task(coro)
self.button_presses.append(task)