2012-11-12 3 views
0

다음은 제 코드입니다. 질문에 대한 답변이 있으면 다른 질문을 시작할 수 없습니다.Python 메시지 상자

나는 앱에서 묻는 질문 목록이 있지만 여기에 모두 게시하는 것은 의미가 없습니다. 나는 계속 질문하는 법을 알아낼 수 없다.

from tkinter import * 
from random import randint 
from tkinter import ttk 


def correct(): 
    vastus = ('correct') 
    messagebox.showinfo(message=vastus) 



def first(): 
    box = Tk() 
    box.title('First question') 
    box.geometry("300x300") 

    silt = ttk.Label(box, text="Question") 
    silt.place(x=5, y=5) 

    answer = ttk.Button(box, text="Answer 1", command=correct) 
    answer.place(x=10, y=30, width=150,) 


    answer = ttk.Button(box, text="Answer 2",command=box.destroy) 
    answer.place(x=10, y=60, width=150) 


    answer = ttk.Button(box, text="Answer 3", command=box.destroy) 
    answer.place(x=10, y=90, width=150) 



first() 
+0

당신이 당신의 코드 추출을 확장 할 수 있습니다 상자? – poke

+0

정답에 응답하면 messagebox를 호출합니다. 그러나 나는 당신의 질문을 이해할 수 없습니다. – user1794625

+0

신경 쓰지 마라, 첫 번째 버튼에서'command = correct' 부분을 놓쳤다. – poke

답변

1

내가 말할 수있는 것부터, 당신은 매우 모호합니다.

tkMessageBox.showinfo() 

무엇입니까?

0

올바른 기능에서 다음 질문 만 실행하거나 QA 대화 상자를 좀 더 추상화하면 훨씬 유연해질 수 있습니다. 나는 약간 지루했다, 그래서 나는 (현재 그러나 불과 3 답변) 질문의 동적를 지원 뭔가 내장 : 실제로`correct` 즉 메시지의 호출을 포함하는

from tkinter import ttk 
import tkinter 
import tkinter.messagebox 

class Question: 
    def __init__ (self, question, answers, correctIndex=0): 
     self.question = question 
     self.answers = answers 
     self.correct = correctIndex 

class QuestionApplication (tkinter.Frame): 
    def __init__ (self, master=None): 
     super().__init__(master) 
     self.pack() 

     self.question = ttk.Label(self) 
     self.question.pack(side='top') 

     self.answers = [] 
     for i in range(3): 
      answer = ttk.Button(self) 
      answer.pack(side='top') 
      self.answers.append(answer) 

    def askQuestion (self, question, callback=None): 
     self.callback = callback 
     self.question['text'] = question.question 

     for i, a in enumerate(question.answers): 
      self.answers[i]['text'] = a 
      self.answers[i]['command'] = self.correct if i == question.correct else self.wrong 

    def correct (self): 
     tkinter.messagebox.showinfo(message="Correct") 
     if self.callback: 
      self.callback() 

    def wrong (self): 
     if self.callback: 
      self.callback() 

# configure questions 
questions = [] 
questions.append(Question('Question 1?', ('Correct answer 1', 'Wrong answer 2', 'Wrong answer 3'))) 
questions.append(Question('Question 2?', ('Wrong answer 1', 'Correct answer 2', 'Wrong answer 3'), 1)) 

# initialize and start application loop 
app = QuestionApplication(master=tkinter.Tk()) 
def askNext(): 
    if len(questions) > 0: 
     q = questions.pop(0) 
     app.askQuestion(q, askNext) 
    else: 
     app.master.destroy() 

askNext() 
app.mainloop() 
+0

슈퍼 큰 고마워요. – user1794625

+1

@ user1794625 유용한 답변을 upvote하고 문제가 해결되면 하나를 수락하십시오. – poke