1

I use Tkinter and have an entry and a button "search". It works well if we enter a text in the entry and hit the search button. It called a function named search_country. However, I would like to call the search_country if we hit the search button or just press enter. Then I bind this : entry.bind('', search_country) and it showed that error.

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)

I try many ways but I get only one of two: hit the button search or press enter in the entry. I need two ways work correctly.

Thank you.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

1 Answers1

1

When you do

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

The binded function gets event as a parameter.

So you might need to add this parameter in your binded function.

def search_country(event): 

Some docs https://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

Tom Wojcik
  • 5,471
  • 4
  • 32
  • 44
  • 2
    As the same function is called when the `Search` button is clicked which expects no argument on the callback function, then it will raise error. Change the function signature to `def search_country(event=None):` instead. – acw1668 May 11 '20 at 04:21
  • Thank you very much for your answer. I will read the docs carefully and try again. – Tuyen Nguyen May 11 '20 at 14:49