0

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

我有一个应用程序提出的问题列表,但在这里发布它们是没有意义的。我只是不知道如何继续提问。

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

2 回答 2

1

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

tkMessageBox.showinfo()

是你在找什么?

于 2012-11-12T11:31:00.243 回答
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()
于 2012-11-12T12:28:47.320 回答