简体   繁体   中英

TKinter running through a loop

For a basic example, suppose I want to run a list of questions and for each question i want a button to be pressed which will append a value "yes" or "no" to the list.

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()

How can i make the window stay open and display all the questions until there is a complete list of answers?

You can write a function like the following:

#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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM