Tkinter askquestion dialog box Tkinter askquestion dialog box tkinter tkinter

Tkinter askquestion dialog box


The problem is your if-statement. You need to get the result from the dialog (which will be 'yes' or 'no') and compare with that. Note the 2nd and 3rd line in the code below.

def deleteme():    result = tkMessageBox.askquestion("Delete", "Are You Sure?", icon='warning')    if result == 'yes':        print "Deleted"    else:        print "I'm Not Deleted Yet"

Now for as to why your code seems to work: In Python a large number of types can be used in contexts where boolean values are expected. So for instance you can do:

arr = [10, 10]if arr:    print "arr is non-empty"else:    print "arr is empty"

The same thing happens for strings, where any non-empty string behaves like True and an empty string behaves like False. Hence if 'yes': always executing.


Below is the code to ask question in messagebox of exit window and then exit if user press Yes.

from tkinter import  *from tkinter import messageboxroot=Tk()def clicked():  label1=Label(root,text="This is text")  label1.pack()def popup():  response=messagebox.askquestion("Title of message box ","Exit Programe ?",   icon='warning')  print(response)   if   response == "yes":      b2=Button(root,text="click here to exit",command=root.quit)      b2.pack()  else:    b2=Button(root,text="Thank you for selecting no for exit .")    b2.pack()button=Button(root,text="Button click",command=clicked)button2=Button(root,text="Exit Programe ?",command=popup)button.pack()button2.pack()root.mainloop()