[issue27546] Integrate tkinter and asyncio (and async)

Terry J. Reedy report at bugs.python.org
Tue Jul 26 01:41:16 EDT 2016


Terry J. Reedy added the comment:

[On #27579, Lodovic Gasc volunteered to open and help with an issue on the github asyncio-doc project.]

Ludovic, here is my current suggestion for the asyncio tutorial or how-to.

Adding a Tkinter GUI to an Asyncio Program
-------------------------------------------

Assume that the tkinter Tk instance is called 'root' and the asyncio loop is called 'loop'. Add a callback loop like the following.

def tk_update():
    root.update()
    loop.call_soon(tk_update)  # or loop.call_later(delay, tk_update)

Call tk_update before each loop.run_forever() or loop.run_until_complete(...) call. Do not call root.mainloop() or root.quit().  Loop.stop() or completion of run_until_complete will stop the tk_update loop.  This method is used in the following example.
---

Follow this with loop_tk2.py as an example.  It is carefully written to be a template that people can build on.  Some notes:

0. I REALLY like being able to write tkinter animations with async for.

1. I initially called loop.close immediately after loop.stop in App.close.  This is a natural think for a tkinter programmer to do.  But it does not work because loop.stop does not *immediately* take effect, and loop.close will typically get called before it does.  Now I understand that loop.close() must physically follow loop.run_xxx() to ensure that it temporally follows the actual stopping of the loop.

2. I did not initially make a list of tasks.  This worked once, but on the next run I got a "Task was destroyed but it is pending!:" message.  Cancelling all 'forever' tasks makes for a clean shutdown.

If/when you open a thread, please post url here and I will subscribe.

[Guido, I will adapt this example for crawl.py.]

----------
nosy: +Ludovic.Gasc
Added file: http://bugs.python.org/file43887/loop_tk2.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27546>
_______________________________________


More information about the Python-bugs-list mailing list