tkinter MP working like a charm
Wu Xi
news at luegenpresse.edu
Sat Dec 30 19:57:00 EST 2017
from tkinter import * # I cant see anything wrong with this , it works like a charm
from tkinter import messagebox # is there a serious concern doing things his way ?
import asyncio , threading , random # goal is multi tasking with the GUI not freezing before loop completed
# which is being achieved here !
def _asyncio_thread(async_loop):
async_loop.run_until_complete(do_urls())
def do_work(async_loop):
""" Button-Event-Handler starting stuff """
threading.Thread(target=_asyncio_thread, args=(async_loop,)).start()
async def one_url(url):
""" One task. """
sec = random.randint(1, 8)
await asyncio.sleep(sec )
return 'url: {} --- sec: {}'.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 submain(async_loop):
root = Tk()
b1 = Button(master=root, text='do work', command= lambda:do_work( async_loop)).pack()
b2 = Button(master=root, text='Frozen?', command=do_nofreeze ).pack()
root.mainloop()
if __name__ == '__main__':
async_loop = asyncio.get_event_loop() # all in this loop
submain(async_loop)
More information about the Python-list
mailing list