2012-11-12 36 views
0

下面是我的代码,如果问题得到解答,我不能让它开始另一个问题。Python messagebox

我有一个应用程序询问的问题列表,但在这里发布它们都是毫无意义的。我无法弄清楚如何继续提问。

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

你能扩大你的代码片段实际上包括为'correct'即您的留言呼叫框? – poke

+0

当正确答案时,它调用消息框。但我不明白你的问题。 – user1794625

+0

没关系,我错过了第一个按钮上的'command = correct'部分。 – poke

回答

1

从我可以告诉你,你的问题是非常含糊。

tkMessageBox.showinfo() 

是你在找什么?

0

您可以在正确的函数内运行下一个问题,或者如果您更多地抽象QA对话框,可以使它更加灵活。我有点无聊,所以我建立的东西,支持的问题,动态数字(只有3个答案目前虽然):

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