'''Proof of concept 2: integrate tkinter and async iterator. Terry Jan Reedy, 2016 July 19 Run with 'python -i' or from IDLE editor to keep tk window alive. This version uses async for loop and async timer to update tk label. See tkloop.py for other comments. ''' import asyncio import tkinter as tk import datetime root = tk.Tk() loop = asyncio.SelectorEventLoop() #loop = asyncio.ProactorEventLoop() #loop = asyncio.get_event_loop() asyncio.set_event_loop(loop) class atimer(): "Yield n or n+1 ticks (None) at intervals." def __init__(self, n, interval, tick0=False): "Yield initial tick if tick0." self.interval = interval self.tick0 = tick0 self.end_time = loop.time() + (n + .9) * interval async def __aiter__(self): return self async def __anext__(self): if self.tick0: self.tick0 = False return await asyncio.sleep(self.interval) if loop.time() <= self.end_time: return else: raise StopAsyncIteration def flipbg(widget, color): bg = widget['bg'] print('click', bg, loop.time()) widget['bg'] = color if bg == 'white' else 'white' async def display_date(n, interval): hello = tk.Label(root, text='Hello World', bg='white') flipper = tk.Button(root, text='Change hello background', bg='yellow', command=lambda: flipbg(hello, 'red')) label = tk.Label(root) hello.pack() flipper.pack() label.pack() root.update() async for tick in atimer(n, interval, tick0=True): print(datetime.datetime.now()) label['text'] = datetime.datetime.now() def tk_update(): root.update() loop.call_later(.01, tk_update) tk_update() loop.run_until_complete(display_date(10, 1.0)) loop.close()