[英]TKinter running through a loop
对于一个基本示例,假设我要运行一个问题列表,并且对于每个问题,我都希望按下一个按钮,该按钮会将值“是”或“否”添加到列表中。
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()
如何使窗口保持打开状态并显示所有问题,直到有完整的答案列表?
您可以编写如下函数:
#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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.