Python消息框

时间:2012-11-12 11:16:35

标签: python tkinter

以下是我的代码,如果问题得到解答,我无法启动另一个问题。

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

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

2 个答案:

答案 0 :(得分:1)

从我所知道的,你的问题很模糊。

tkMessageBox.showinfo()

您正在寻找什么?

答案 1 :(得分: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()