TKinter通过循环运行

时间:2017-11-10 18:25:08

标签: python tkinter

对于一个基本的例子,假设我想运行一个问题列表,对于每个问题,我想要一个按钮被按下,它会在列表中附加一个值“是”或“否”。

window = tk.Tk()
app=tk.Frame(window)
app.grid()

response_list = []

y_button = tk.Button(app,text="yes", command=lambda x="yes": appendResponse(x))

n_button = tk.Button(app,text="no", command=lambda x="no": appendResponse(x))

questions=["q1","q2","q3"]

window.mainloop()

如果有完整的答案列表,我怎样才能让窗口保持打开并显示所有问题?

1 个答案:

答案 0 :(得分:0)

您可以编写如下函数:

#STARTS HERE
#Label with question
lbl1 = tk.Label(app, text="Are you a human?")
lbl1.grid()

def appendResponse(resp):
    global response_list

    questionNo = len(response_list)

    if questionNo % 3 == 0:
        lbl1.configure(text="Is this a valid question?")

    elif questionNo % 3 == 1:
        lbl1.configure(text="Is there a better way to do this?")

    elif questionNo % 3 == 2:
        lbl1.configure(text="Is this what you wanted to do?")

    response_list.append(resp)

#FINISHES HERE