如何使用按钮在Tkinter中创建测验

时间:2019-03-11 09:38:38

标签: python sql tkinter

我正在尝试进行测验,以问另一个问题,并在按下按钮后加载与该问题相关的答案。它加载第一个问题,一个按钮具有正确的答案,而三个按钮具有错误的答案(本来应该是多重选择,所以应该这样做),但是当按下一个按钮时,新的问题不会加载。它说我按下按钮时未定义ID。

那么我如何循环它,以便出现新的ID,从而加载新的问题。多项选择有效,只是循环而已。

class Page2(Page):
    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        self.Question_Counter = 0
        self.Correct_Answers = 0
        self.getQuest
        self.AnswerQuestion

        self.ID = random.randint(1,100)
        QuestionSet.add(self.ID)
        self.QuestionAsked = self.getQuest(self.ID)
        QuestionSet.add(self.QuestionAsked)
        Answers = [self.GetAnswer(self.ID)]
        self.CorrectAnswer = self.GetAnswer(self.ID)
        for ID in range(3):
            new_id = random.randint(1,100)
            while new_id in Answers:
                new_id = random.randint(1,100)
            Answers.append(self.GetAnswer(new_id))
        self.OriginalAnswers = Answers[:]
        print(self.CorrectAnswer)
        print(self.GetAnswer(self.ID))

        l1 = tk.Label(self, text = self.QuestionAsked)
        l1.grid(row = 2, column = 2, pady = 100)

        l2 = tk.Label(self, text ="/50")
        l2.grid(row = 1, column = 3)

        myID = random.randint(0,3)
        b1 = tk.Button(self, text = Answers[myID], command = self.AnswerQuestion)
        b1.grid(row = 3, column = 1)
        del Answers[myID]

        myID = random.randint(0,2)
        b2 = tk.Button(self, text = Answers[myID], command = self.AnswerQuestion)
        b2.grid(row = 4, column = 1, pady = 10)
        del Answers[myID]

        myID = random.randint(0,1)
        b3 = tk.Button(self, text = Answers[myID], command = self.AnswerQuestion)
        b3.grid(row = 3, column = 3)
        del Answers[myID]

        b4 = tk.Button(self, text = Answers[0], command = self.AnswerQuestion)
        b4.grid(row = 4, column = 3, pady = 10)
        del Answers[0]

    def getQuest(self,ID):
        if self.Question_Counter < 50:
            RandomInt = random.randint(1,100)
            new_db = sqlite3.connect("QuestionsDatabase.db")
            c= new_db.cursor()
            return (c.execute("SELECT Question FROM Questions WHERE QuestionID = {}".format(ID)).fetchone()[0])
            self.Loading(self)

    def GetAnswer(self,ID):
        new_db = sqlite3.connect("QuestionsDatabase.db")
        c= new_db.cursor()
        return (c.execute("SELECT ANSWER FROM ANSWERS WHERE AnswerID = {}".format(ID)).fetchone()[0])  

    def AnswerQuestion(self):
        if self.CorrectAnswer == self.GetAnswer(self.ID):
            self.Correct_Answers += 1
        self.Question_Counter += 1
        self.getQuest(self,ID)

0 个答案:

没有答案