tkinter askyesno message box behaviour

user1478335 picture user1478335 · May 13, 2013 · Viewed 22.4k times · Source
messagebox.askyesno(0.0,'"{0}"{1} \n {2}\n'.format(search_analyte.get(),' is  not in the database.','Add,if appropriate'))
        print (str(messagebox.askyesno()))
        if messagebox.askyesno() == True:
            open_second()
        else:
            open_first()

In this snippet of code the askyesno messagebox opens. If I click 'yes' once, nothing happens, if I click it a second time, 'True' is printed to the console(just added to see what I was returning), if I click it a third time, the messagebox closes and the correct notebook page opens. The same behaviour occurs with 'no'. First click, nothing, messagebox remains open but text within the message box disappears leaving only yes, no. The second time False is printed to the console, box remains open, third time, the box closes and the correct notepook page is open. Can someone explain what I have done incorrectly please

Answer

Bryan Oakley picture Bryan Oakley · May 15, 2013

In the code you posted, you are opening three dialogs. Every time you call askyesno you will get a dialog. You need to call askyesno once, save the value that it returns, then use that value in your test.

result = messagebox.askyesno(...)
if result:
    ...