tkinter MP working like a charm - almost...
Wu Xi
news at luegenpresse.edu
Mon Jan 1 08:33:00 EST 2018
from tkinter import * ; from tkinter import messagebox
import asyncio , random
async def one_url(url):
""" This is a multi threaded tkinter demo where tasks run in the background and aim to not freeze the tk GUI, py v3.7"""
sec = random.randint(1, 8)
await asyncio.sleep(sec )
return 'url: {} took {} seconds'.format(url, sec)
async def do_urls():
""" Creating and starting 10 tasks. """
tasks = [one_url(url) for url in range(10)]
completed, pending = await asyncio.wait(tasks)
results = [task.result() for task in completed]
print('\n'.join(results))
def do_nofreeze(): messagebox.showinfo(message='see, Tkinter is still responsive.')
def widgets(master): Button(master, text='GUI Frozen?', command=do_nofreeze).pack()
def tk_update(interval):
""" A production version of this should make it possible to cancel. However, when loop stops, updates will stop. T. Reedy"""
loop.call_later( interval, root.update )
root.update()
if __name__ == '__main__':
root = Tk()
widgets(root)
loop = asyncio.get_event_loop()
tk_update(.05)
loop.run_until_complete(do_urls())
# wow! no call to root.mainloop() anymore. somehow like in IDLE - but it freezes now :-(
More information about the Python-list
mailing list