2017-12-05 14 views
0

버튼을 누를 때 다른 단어를 나타내는 tkinter을 가진 프로그램을 만들려고합니다. 그럼 내 질문은 다음과 같습니다 : 버튼이 있다고 말합니다. next question 버튼을 누를 때마다 현재 화면에있는 질문이 다음 화면으로 변경됩니다 (현재 Q1 ->button 누름 -> Q2가 Q1 대체 됨). 각 질문마다 다른 버튼이 아닌 하나의 버튼 만 갖고 싶습니다. 이 작업을 수행하려면 어떻게해야합니까? lists을 사용해 보았지만 제대로 작동하지 않았습니다.파이썬 tkinter 한 버튼에 대한 여러 명령

미리 감사드립니다.

+0

당신이 뭘하려 최소한의 버전을 제공하십시오. – Nae

답변

1

가장 간단한 해결책은 질문을 목록에 넣고 전역 변수를 사용하여 현재 질문의 색인을 추적하는 것입니다. "다음 질문"버튼은 단순히 색인을 증가시키고 다음 질문을 표시해야합니다.

클래스를 사용하는 것이 전역 변수보다 좋지만 예제를 짧게 유지하기 위해 클래스를 사용하지 않을 것입니다.

예 :

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

정말 고마워요! –