'Unable to run tkinter within asyncio loop
I can't seem to run tkinter whithin an asyncio loop. As far as I can see that the problem comes from running the tkinter mainloop which clashes with and stops the asyncio loop.
import asyncio
import tkinter as tk
import random
async def firstWorker():
while True:
print("First Worker Executed")
await asyncio.sleep(1)
async def update():
while True:
l.config(text=str(random.random()))
root.after(1000, update())
root.mainloop()
await asyncio.sleep(1)
root = tk.Tk()
l = tk.Label(text='0')
l.pack()
root.after(1000, update())
asyncio.ensure_future(firstWorker())
asyncio.ensure_future(update())
asyncio.get_event_loop().run_forever()
There are a couple of questions / answers that deal with running asyncio loop and tkinter gui but they are application specific and I haven't been able to re-use the code so I thought I would ask the question using the most simple standalone code example below which will help others in the future.
(I'm essentially combining an asyncio event loop from this tutorial and the tkinter example provided here.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
