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

问题描述 投票:1回答:1

我使用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。我需要两种方式才能正常工作。

谢谢。

python button tkinter bind tkinter-entry
1个回答
0
投票

执行时

entry.bind('<Return>', search_country)

绑定的函数将event作为参数。

因此,您可能需要在绑定的函数中添加此参数。

def search_country(event): 

某些文档https://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

© www.soinside.com 2019 - 2024. All rights reserved.