tKinter对话框按钮无响应

时间:2015-09-26 04:44:24

标签: python user-interface tkinter dialog

我是tKinter的新手。我尝试了这个代码,但它没有用。当我单击任何按钮(是/否)时,对话框不会关闭,而打印语句则不会 打印。我知道在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()

我该如何纠正?

2 个答案:

答案 0 :(得分:1)

您必须在' if'下使用缩进。声明。即:

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

答案 1 :(得分:0)

返回的值为public class ECApiManagerTests { InstrumentationTestCase runnerHelper = new InstrumentationTestCase(); private final String userEmail = "removed"; private final String passWord = "removed"; String userHash; @Test public void testPOST()throws Throwable{ final AsyncHttpClient httpClient = new AsyncHttpClient(); final CountDownLatch signal = new CountDownLatch(1); runnerHelper.runTestOnUiThread(new Runnable() { @Override public void run() { RequestParams params = new RequestParams(); params.put("email", userEmail); params.put("password", passWord); httpClient.post("https://edu.chat/api/login/", params, new AsyncHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { //called when response code 200 userHash = new String(responseBody); } @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { } @Override public void onFinish() { signal.countDown(); } }); } }); try { signal.await(30, TimeUnit.SECONDS); // wait for callback } catch (InterruptedException e) { e.printStackTrace(); } try { System.out.println(userHash); JSONObject obj = new JSONObject(userHash); Assert.assertEquals("true", obj.getString("success")); } catch (JSONException e) { e.printStackTrace(); } } (全部小写),但您尝试对'yes'进行检查,因此无法打印。尝试针对'Yes'进行检查。

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

示例 -

root.mainloop()

请注意,这会将import tkinter.messagebox answer=tkinter.messagebox.askquestion('Question','what is your name?') if answer=='yes': print('I am King') 打印到控制台中。

回答评论中的问题 -

  

有没有办法可以隐藏名为tk--的对话框,这是后台的第二个?

您必须使用'I am King'创建应用程序,然后才能使用Tk()。示例 -

root.withdraw()