TypeError:search_country()接受0个位置参数,但给出了1个

2024-05-19 19:18:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Tkinter,有一个条目和一个“搜索”按钮。如果我们在条目中输入一个文本并点击搜索按钮,效果会很好。它调用了一个名为search\u country的函数。 但是,如果我们点击搜索按钮或只按回车键,我想呼叫搜索国家。然后我绑定这个:entry.bind(“”,search\u 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键。我需要两种方法正确工作

多谢各位


Tags: 方法searchindexbindtkinter条目columnbutton