Tkinter Askquestion对话框 [英] Tkinter askquestion dialog box

查看:287
本文介绍了Tkinter Askquestion对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试向Tkinter中的删除按钮添加一个Askquestion对话框.目前,我有一个按钮,可以在按下该文件夹后删除该文件夹的内容,我想添加一个是/否确认问题.

I have been trying to add an askquestion dialog box to a delete button in Tkinter. Curently I have a button that deletes the contents of a folder once it is pressed 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()

每次我运行此命令时,即使我按否",也会得到已删除"语句.可以将if语句添加到tkMessageBox吗?

Everytime I run this I get the "Deleted" statement even if I press "No". Can an if statement be added to a tkMessageBox?

推荐答案

问题是您的if语句.您需要从对话框中获取结果(将为'yes''no')并与之进行比较.请注意以下代码中的第二行和第三行.

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"

现在说明为什么代码似乎可以工作:在Python中,可以在需要布尔值的上下文中使用大量类型.因此,例如,您可以这样做:

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"

对于字符串也是如此,其中任何非空字符串的行为类似于True,而空字符串的行为类似于False.因此,if 'yes':总是执行.

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.

这篇关于Tkinter Askquestion对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆