Jef*_*eff 8 python tkinter tkmessagebox
我一直在尝试向Tkinter中的删除按钮添加一个askquestion对话框.我有一个按钮,一旦按下它就会删除文件夹的内容我想添加是/否确认问题.
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()
Run Code Online (Sandbox Code Playgroud)
每次我运行这个,即使按"否",我也会收到"已删除"声明.可以将if语句添加到tkMessageBox吗?
JPv*_*rwe 21
问题是你的if陈述.您需要从对话框中获取结果(将是'yes'或'no')并与之进行比较.请注意下面代码中的第2行和第3行.
def deleteme():
result = tkMessageBox.askquestion("Delete", "Are You Sure?", icon='warning')
if result == 'yes':
print "Deleted"
else:
print "I'm Not Deleted Yet"
Run Code Online (Sandbox Code Playgroud)
现在,为什么你的代码似乎有用:在Python中,可以在需要布尔值的上下文中使用大量类型.例如,您可以这样做:
arr = [10, 10]
if arr:
print "arr is non-empty"
else:
print "arr is empty"
Run Code Online (Sandbox Code Playgroud)
对于字符串也会发生同样的事情,其中任何非空字符串都表现得像,True而空字符串的行为就像False.因此if 'yes':总是执行.