How can I send a message down a websocket running in a thread from tkinter? How can I send a message down a websocket running in a thread from tkinter? tkinter tkinter

How can I send a message down a websocket running in a thread from tkinter?


It turns out that the best way of solving this is not to use threads.

This post helped me solve it. It shows that you can run, in a coroutine, one iteration of the tkinter mainloop:

async def do_gui(root,interval=0.05):    while True:        root.update()        await asyncio.sleep(interval)

However, the best way of getting a tkinter event to spawn a websocket message is to use asyncio.queue. Making the tkinter callback add an item to the queue using put_nowait() and having a coroutine which runs at the same time as do_gui which uses await queue.get() to get the message from the queue works for me.