Question

I'm making a quiz app with tkinter and i want each question in a new window, but after I create a new window i can't figure out how to destroy the previous window. Here is a rough simplified excerpt of my code:

from tkinter import *

class q1:
    def __init__(self, master):
        self.master = master
        Label(self.master, text='What is 3 + 3?').grid()
        self.option_1 = Button(self.master, text='5', command = self.incorrect)
        self.option_1.grid()
        self.option_2 = Button(self.master, text='6', command = self.correct)
        self.option_2.grid()

    def correct(self):
        self.option_1.config(state=DISABLED)
        self.option_2.config(state=DISABLED)
        Label(self.master, text='Correct').grid()
        Button(self.master, text='Next Question', command = self.nextq).grid()

    def incorrect(self):
        self.option_1.config(state=DISABLED)
        self.option_2.config(state=DISABLED)
        Label(self.master, text='Incorrect').grid()
        Button(self.master, text='Next Question', command = self.nextq).grid()

    def nextq(self):
        q2(Toplevel(self.master))

class q2:
    def __init__(self, master):
        self.master = master
        Label(self.master, text='Question 2').grid()

def window():
    root = Tk()
    q1(root)
    root.mainloop()

if __name__ == '__main__':
    window()
Was it helpful?

Solution

UPDATED VERSION

from Tkinter import *
import random

class Ask:
    def __init__(self, parent,question,right,wrong):
        self.answer=right
        self.top=top=Toplevel(parent)
        Label(self.top, text=question,width=25).grid()
        a1=random.choice([right,wrong])
        self.option_1 = Button(self.top, text=a1, width=25,command = lambda: self.check(1))
        self.option_1.grid()
        a2=right
        if a1==right:
            a2=wrong
        self.option_2 = Button(self.top, text=a2, width=25, command = lambda: self.check(2))
        self.option_2.grid()

    def check(self,pressed):
        if pressed==1:
            ans=self.option_1['text']
        else:
            ans=self.option_2['text']
        if ans==self.answer:
            self.correct()
        else:
            self.incorrect()

    def correct(self):
        self.option_1.config(state=DISABLED)
        self.option_2.config(state=DISABLED)
        Label(self.top, text='Correct').grid()
        Button(self.top, text='Next Question', command = self.top.destroy).grid()

    def incorrect(self):
        self.option_1.config(state=DISABLED)
        self.option_2.config(state=DISABLED)
        Label(self.top, text='Incorrect').grid()
        Button(self.top, text='Next Question', command = self.top.destroy).grid()

class QUIZ:
    def __init__(self, questionsdict):
        self.root=Tk()

        self.root.withdraw()
        self.questions(questionsdict)
        self.root.deiconify()
        c=Button(self.root, text='Click me to exit', command=self.root.destroy, width=30).grid(row=2, sticky='ew')
        Label(self.root, text='Quiz is Complete', width=30, background='red').grid(row=0, sticky='ew')
        mainloop()

    def questions(self,questionsdict):
        for k in questionsdict.keys():
            right=questionsdict[k][0]
            wrong=questionsdict[k][1]
            b=Ask(self.root, k, right,wrong )
            self.root.wait_window(b.top)

put quiz questions in a dictionary in the format: {question:[correctchoice,incorrectchoice]} and use that as the parameter when calling the Question Class

questions={'What is 3+3':['6','8'], 'What is the capital of Alaska': ['Juno','Anchorage']}
QUIZ(questions)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top