0

Below is my code, I can't make it start another question if a question is answered.

I have a list of questions that the app asks, but it is pointless to post them all here. I just can't figure out how to keep on asking questions.

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()
5
  • Can you expand your code extract to actually include a call to correct i.e. your message box? Commented Nov 12, 2012 at 11:19
  • When correct is answered then it calls the messagebox. But I can't understand your question. Commented Nov 12, 2012 at 11:22
  • Nevermind, I missed the command=correct part on the first button. Commented Nov 12, 2012 at 11:30
  • Can’t you just call the next question from within the correct function? Commented Nov 12, 2012 at 11:43
  • I have tried that but I couldn't get it to work Commented Nov 12, 2012 at 11:46

2 Answers 2

1

from what I can tell, you question is very vague.

tkMessageBox.showinfo()

is what you're looking for?

Sign up to request clarification or add additional context in comments.

Comments

0

You could either just run the next question from within your correct function, or if you abstract your QA dialog a bit more, you could probably make it a lot more flexible. I was a bit bored, so I built something that supports a dynamic number of questions (with only 3 answers currently though):

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

1 Comment

@user1794625 Please remember to upvote helpful answers, and accept one if it solved your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.