我使用Tkinter并有一个条目和一个按钮“搜索”。如果我们在条目中输入文本并点击搜索按钮,则效果很好。它调用了一个名为search_country的函数。但是,如果我们按搜索按钮或仅按Enter,我想致电search_country。然后我将其绑定:entry.bind('',search_country)并显示该错误。
entry = Entry(win)
entry.grid(row=0, column=1)
entry.bind('<Return>', search_country)
button_search = Button(f2, text="Search", command= search_country)
button_search.grid(row=0, column=2)
def search_country():
search_ = " ".join(entry.get().split()).title().replace('And','&')
entry.delete(0,"end")
if search_ in countries:
country_index= countries.index(search_)
listbox.selection_clear(0, END)
listbox.select_set(country_index)
showimg(country_index)
我尝试了很多方法,但只有两种方法之一:按下按钮搜索或在条目中按Enter。我需要两种方式才能正常工作。
谢谢。
执行时
entry.bind('<Return>', search_country)
绑定的函数将event
作为参数。
因此,您可能需要在绑定的函数中添加此参数。
def search_country(event):
某些文档https://effbot.org/tkinterbook/tkinter-events-and-bindings.htm