가장 간단한 해결책은 질문을 목록에 넣고 전역 변수를 사용하여 현재 질문의 색인을 추적하는 것입니다. "다음 질문"버튼은 단순히 색인을 증가시키고 다음 질문을 표시해야합니다.
클래스를 사용하는 것이 전역 변수보다 좋지만 예제를 짧게 유지하기 위해 클래스를 사용하지 않을 것입니다.
예 :
import Tkinter as tk
current_question = 0
questions = [
"Shall we play a game?",
"What's in the box?",
"What is the airspeed velocity of an unladen swallow?",
"Who is Keyser Soze?",
]
def next_question():
show_question(current_question+1)
def show_question(index):
global current_question
if index >= len(questions):
# no more questions; restart at zero
current_question = 0
else:
current_question = index
# update the label with the new question.
question.configure(text=questions[current_question])
root = tk.Tk()
button = tk.Button(root, text="Next question", command=next_question)
question = tk.Label(root, text="", width=50)
button.pack(side="bottom")
question.pack(side="top", fill="both", expand=True)
show_question(0)
root.mainloop()
당신이 뭘하려 최소한의 버전을 제공하십시오. – Nae