在tkinter.button中传递异步函数

 收藏

我正在制作一个应用程序,以从api请求一些数据,并希望集成一个GUI。 如果需要,我可以提供wilib2的代码

from tkinter import *
import asyncio
import aiohttp
import wilib2
entries = []
class WarframeEntry():
    def __init__(self, entry_name):
        self.name = entry_name

window = Tk()
window.title("Test App")
window.geometry('600x450')
lbl = Label(window, text="Enter Item Id:")
lbl.grid(column=0, row=0)
textBox = Entry(window, width=20)
textBox.grid(column=1, row=0)
async def updateDisplay():
    async with aiohttp.ClientSession() as session:
        htmls = await wilib2.fetch_all(session, entries)
        for item in htmls:
            text = Label(window, text=item['item_name'])
async def addNewEntry():
    entries.add(textBox.get())
    await updateDisplay()
btn = Button(window, text="Search", command=lambda: await asyncio.ensure_future(addNewEntry))
btn.grid(column=3, row=0)
window.mainloop()
回复