Tkinter Poll Dialog Box

I am trying to add the askquestion dialog box to the delete button in Tkinter. In the course, I have a button that deletes the contents of a folder after clicking it. I would like to add a yes / no confirmation question.

import Tkinter
import tkMessageBox

top = Tkinter.Tk()
def deleteme():
    tkMessageBox.askquestion("Delete", "Are You Sure?", icon='warning')
    if 'yes':
        print "Deleted"
    else:
        print "I'm Not Deleted Yet"
B1 = Tkinter.Button(top, text = "Delete", command = deleteme)
B1.pack()
top.mainloop()

Every time I run this, I get a β€œDeleted” statement, even if I click β€œNo”. Can an if statement be added to tkMessageBox?

+5
source share
1 answer

The problem is your if-statement. You need to get the result from the dialog (which will be 'yes'or 'no') and compare with it. Pay attention to the second and third lines 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"

, : Python , . , , :

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

, True, False. , if 'yes': .

+15

All Articles