Reputation: 105
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()
Upvotes: 0
Views: 1738
Reputation: 388313
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()
Upvotes: 0
Reputation: 924
from what I can tell, you question is very vague.
tkMessageBox.showinfo()
is what you're looking for?
Upvotes: 1