# import
from tkinter import *

# start
class Question:
    def __init__(self, question, answers, correctLetter):
        self.question = question
        self.answers = answers
        self.correctLetter = correctLetter

    def check(self, letter, view):
        global right
        if(letter == self.correctLetter):
            label = Label(view, text="Right!")
            right += 1
        else:
            label = Label(view, text="Wrong!")
        label.pack()
        view.after(1000, lambda *args: self.unpackView(view))

    def getView(self, window):
        view = Frame(window)
        Label(view, text=self.question).pack()
        Button(view, text=self.answers[0], command=lambda *args: self.check("A", view)).pack()
        Button(view, text=self.answers[1], command=lambda *args: self.check("B", view)).pack()
        Button(view, text=self.answers[2], command=lambda *args: self.check("C", view)).pack()
        Button(view, text=self.answers[3], command=lambda *args: self.check("D", view)).pack()
        return view

    def unpackView(self, view):
        view.pack_forget()
        askQuestion()

def askQuestion():
    global questions, window, index, button, right, number_of_questions
    if(len(questions) == index + 1):
        Label(window, text="Thank you for answering the questions. " + str(right) + " of " + str(number_of_questions) + "\n questions answered right").pack()
        return
    button.pack_forget()
    index += 1
    questions[index].getView(window).pack()

def test2():
    intro = entry_box.get()
    entry_box.delete(0, END)
    output_box = Message(text=intro)
    output_box.place(x=20, y=30)
    msg = Label(window, text=intro)
    msg.place(x=20, y=30)

entry_box=Entry(text=0)
entry_box.place(x=50, y=80, width=120, height=25)

questions = []
file = open("questions.txt", "r")
line = file.readline()
while(line != ""):
    questionString = line
    answers = []
    for i in range(4):
        answers.append(file.readline())
    correctLetter = file.readline()
    correctLetter = correctLetter[:-1]
    questions.append(Question(questionString, answers, correctLetter))
    line = file.readline()
file.close()
index = -1
right = 0
number_of_questions = len(questions)

window = Tk()
window.title("Our App")
window.geometry("320x240")
button = Button(window, text="Start", command=askQuestion)
button.place(x=100, y=100, width=120, height=25)
button1 = Button(text="Press Me", command=test2)
button1.place(x=100, y=100, width=120, height=25)
#button.pack()
window.mainloop()