Python tKinter对话框按钮没有响应

Python tKinter对话框按钮没有响应,python,user-interface,tkinter,dialog,Python,User Interface,Tkinter,Dialog,我是新来的。我尝试了这个代码,但没有成功。单击任何按钮(是/否)时,对话框不会关闭,打印语句也不会关闭 印刷品。我知道在Swing中,我需要事件来打印此语句。tKinter也一样吗 from tkinter import * import tkinter.messagebox root = Tk() answer=tkinter.messagebox.askquestion('Question','what is your name?') if answer=='Yes': print('I

我是新来的。我尝试了这个代码,但没有成功。单击任何按钮(是/否)时,对话框不会关闭,打印语句也不会关闭 印刷品。我知道在Swing中,我需要事件来打印此语句。tKinter也一样吗

from tkinter import *
import tkinter.messagebox

root = Tk()

answer=tkinter.messagebox.askquestion('Question','what is your name?')
if answer=='Yes':
print('I am King')

root.mainloop()

如何更正它?

返回值将是
'yes'
(全部小写),但您正试图对照
'yes'
检查它,因此它不会打印。试着对照
“是”
检查它

另外,实际的应用程序
root
在您单击
x
按钮之前不会关闭,因为您正在定义
Tk()
应用程序并进入其主循环。如果希望它关闭(并且程序以print语句结束),则不需要
root=Tk()
root.mainloop()

范例-

import tkinter.messagebox

answer=tkinter.messagebox.askquestion('Question','what is your name?')
if answer=='yes':
    print('I am King')
import tkinter.messagebox
from tkinter import Tk

root = Tk()
root.withdraw()
answer=tkinter.messagebox.askquestion('Question','what is your name?')
if answer=='yes':
    print('I am King')
请注意,这会将
“我是国王”
打印到控制台中


在评论中回答问题-


有没有一种方法可以隐藏名为tk的对话框--这是后台的第二个对话框

为此,您必须使用
Tk()
创建应用程序,然后可以在其上使用
root.draw()
。范例-

import tkinter.messagebox
from tkinter import Tk

root = Tk()
root.withdraw()
answer=tkinter.messagebox.askquestion('Question','what is your name?')
if answer=='yes':
    print('I am King')

必须在“if”语句下使用缩进。即:

    if answer.lower() == 'yes': # this proofs against mistakes with user capitalisation
        print ('I am the king')

有没有办法隐藏名为tk的对话框——这是后台的第二个对话框?@BattleDrum我已经添加了一个解决方案,您现在可以检查。